开发者

Jquery selector data retrival in variable, in another different page

开发者 https://www.devze.com 2022-12-20 22:14 出处:网络
How can we use Jquery to read certain page content in a variable on a different page? If for example we want to read div.tag1 from a page into another page but in a variable( ie i dont want to load it

How can we use Jquery to read certain page content in a variable on a different page? If for example we want to read div.tag1 from a page into another page but in a variable( ie i dont want to load it into the page but manipulate it so store it in variable)

Eg: file1.html have(say)

<div class="tag1"> Hello world </div>
<div class="tag2"> Hello Boys </div>
<div class="tag3"> Hello Girls </div>

Now I want page2 to have Jq开发者_如何学编程uery script that will store the content of div.tag1 in a variable.

This is what I need in file2.html : // without loading it to any page ie without using load.

<script type="text/javascript">
var filename = "file1.html";
var myData = // Code to retrieve div.tag1 from file1.html( ie filename)
document.write(myData); // for the time being.. later it will go in Database.

Any help will be appreciated.


The simplest solution is to use load on a newly created element (a hidden element will also work):

var content = $("<div />").load("/page.html div.tag1",
   function(){ // callback
       alert(content.text());
   });
0

精彩评论

暂无评论...
验证码 换一张
取 消