【问题标题】:How to get source code of html page using jquery如何使用jquery获取html页面的源代码
【发布时间】:2012-10-06 21:56:52
【问题描述】:

在 android phonegap 应用程序中,我需要使用 javascript 或 jquery 从 index1.html 获取 index2.html 的脚本。

这里是index1.html代码:

   function nextpage(){

   $.get('index2.html', function(data) {
          $('#div').html(data);
          alert('Load was performed.');
        });
     }

      <input type="button" value="button1" onclick="nextpage()"/> 
      <div id="div"></div>

这里是 index2.html 代码:

function return1()
     {
 alert("welcome to index2.html");
 var data={ "firstName": "John", "lastName": "Doe", "age": 25 };
     }

     <input type="button" value="welcome" onclick="return1()"/> 

从这段代码中,我从 index2.html 中获得了“欢迎”按钮,并附加在 index1.html 的 #div 中。但我需要在 #div 中附加 return1() 并需要获取警报(“欢迎来到index2.html")。那是html页面功能响应。怎么做。请帮助我。提前谢谢。

【问题讨论】:

  • 追加后需要调用return1()函数,你会得到警报。
  • 感谢Roopchand。不附加到div,如何调用return1()
  • 首先需要追加index2.html数据脚本,然后才能调用return1()函数,不追加不能使用其他页面脚本。

标签: android jquery html cordova


【解决方案1】:

我在附加 index2.html 后在您的代码中添加 return1() 函数。

function nextpage(){

   $.get('index2.html', function(data) {
          $('#div').html(data);
          alert('Load was performed.');

              return1(); //call this function to show alert

        });
     }

【讨论】:

  • 好的,谢谢。我只需要从 index2.html 中读取 json。怎么做。我不想要完整的 html 页面。请指导我。请参阅上面 index2.html 代码中的编辑。
  • 如果你想从其他文件中获取json数据,最好使用一个生成json数据的php文件,在你的主页中使用这个json文件url并解码json并使用它,不需要像附加文件一样正如你所做的那样,.....如果你在 phone-Gap 中执行此操作,然后将你的 php 文件放在服务器上,然后在 java-script 中使用服务器 url,或者你可以直接在你的 phone-gap 文件夹中使用 json 格式的文件,用 javascript 读取这个文件。
  • 谢谢Roopchand。但我不应该使用php。只有javascript和jquery。请你提供任何其他解决方案。
  • 是的,这就是为什么我已经写过你可以在你的文件夹中直接使用 json 文件,比如“data.json”,然后你可以通过 java-script/jquery 从这个文件中获取数据。
  • $.getJSON(json_file_URL, function(data) { var json_data = data.item; $('#div_id').text(json_data.firstName); });
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-24
  • 2017-06-06
  • 2023-01-14
  • 2015-04-30
  • 1970-01-01
  • 2014-07-02
相关资源
最近更新 更多