【问题标题】:how can call a html page div from javascript?如何从 javascript 调用 html 页面 div?
【发布时间】:2011-08-31 12:30:12
【问题描述】:

我正在开发 phonegap with android。我正在创建一个登录页面。登录验证后,如果用户被授权,我想跳转到欢迎 div。

这是我的 javascript 页面:- myjavascrpt.js

   var logi=item.id;   
   if(logi>0)
   call welcome div of index.html
    else
    alert("invalid user"); 

                       // in this logi variable i have userid of successful user.
                       // now i want to  
                        // display this id into the div which is exist in my 
                        // html page.
                        // now what should i write here so i can call welcome 
                        // div of html page and i can use 
                        // this value of logi variable.

这是我的 index.html,我想调用它的欢迎 div。

    <html>
      <head></head>
      <body>
        <div data-role="page" data-theme="a" data-url="streaming" id="streaming">
             <h1> do some thing related to streaming</h1>
        </div>  
        **
        <div data-role="page" data-theme="a" data-url="welcome" id="welcome">
        <h1> you are welcome</h1>
        </div>
        **
      </body>
    </html>

所以请告诉我应该使用什么代码来调用 html 页面的 div。

【问题讨论】:

    标签: javascript android html cordova


    【解决方案1】:

    我对从javascript调用html页面div一无所知,但是如果你想通过javascript设置DIV的内容,就可以这样做了:

    在javascript中:

    var d = document.getElementByID("welcome");
    d.innerHTML = logi;
    

    但这会覆盖&lt;h1&gt; you are welcome&lt;/h1&gt; 文本。所以将该行更改为

    <span id="userwelcome"></span><h1>, you are welcome</h1>
    

    和你的javascript到

    var d = document.getElementByID("userwelcome");
    d.innerHTML = logi;
    

    【讨论】:

    • if(logi&gt;0){ var d = document.getElementByID("userwelcome"); d.innerHTML = logi;}
    • 是的,你是对的,但我怎样才能显示 index.html 的 Welcome-div。
    【解决方案2】:

    你可以用简单的 jQuery 做到这一点 -

    if(logi>0) { $('#welcome').html(logi);}
    

    【讨论】:

    • 对不起,先生,我无法使用您的代码跳转我的 html 页面的欢迎 div。
    猜你喜欢
    • 2011-01-30
    • 2019-01-28
    • 1970-01-01
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    相关资源
    最近更新 更多