简体   繁体   English

jQuery从字符串中提取div后,div内容丢失

[英]div content is lost after extracting the div from a string by jQuery

Update: 更新:

I finally figured out why the content of the #output div is empty. 我终于弄清楚了为什么#output div的内容为空。 It is because that the content is retrieved from server and it takes time to do so, so by the time the document is loaded the content of this div is still empty. 这是因为从服务器检索内容需要花费时间,因此在加载文档时,该div的内容仍为空。

Anyone has idea about extract information from the delayed div content by only using JavaScript or jQuery etc. (client side programming)? 有谁想过仅通过使用JavaScript或jQuery等(客户端编程)从延迟的div内容中提取信息?


I have a variable which stores a very large string and its content is html. 我有一个存储很大字符串的变量,其内容是html。 let's call it content. 我们称之为内容。

then 然后

var html = $.parseHTML(content); var html = $ .parseHTML(content);

after this I want to find this chunk (and extract the values from it) 之后,我想找到这个块(并从中提取值)

<div id="output" style="float:left;width:150px;margin:30px 0 0 1px;"><div class="result-title">Caltex/Woolworths St Kilda</div><div class="result-text">price: 152.9c<br>address: 99 Chapel St &amp; Inkerman St, St Kilda East</div><div class="result-title">BP East Prahran</div><div class="result-text">price: 152.9c<br>address: 549 High St &amp; Chatsworth Rd, Prahran</div><div class="result-title">BP Balaclava</div><div class="result-text">price: 152.9c<br>address: 308 Carlisle St &amp; Blenheim St, Balaclava</div><div class="result-title">7 Eleven St Kilda</div><div class="result-text">price: 153.9c<br>address: 154-158 St Kilda Rd &amp; Alma Rd, St Kilda</div><div class="result-title">BP AA Prahran</div><div class="result-text">price: 153.9c<br>address: 500 Malvern Rd, Prahran</div></div>

I tried to loop through it but don't know which attribute should be used to find that chunk. 我试图遍历它,但是不知道应该使用哪个属性来查找该块。

 function onS(data)
           {
               var html = $.parseHTML(data.responseText);
               $.each(html, function (i, ele) {
                   if (ele.nodeName == '#div')
                   {
                       alert('found it!!!');
                   }
               });
               alert("Data Loaded: " + data.responseText);
           }

Please help. 请帮忙。

****Update:**** ****更新:****

Code: 码:

function onS(data)
           {
               // Niclas
               var nodes = $('#output', data.results[0]);
               alert("Data Loaded: " + nodes.html());

               //Adeneo
               var node = $(data.results[0]).find('div#output'); 
               alert("Data Loaded: " + node.html());
           }

The runtime values (please look at innerHTML), it is empty in both ways. 运行时值(请查看innerHTML),两种方式都为空。

(the pic looks like too small, you can press Ctrl + mouse wheel to zoom in/out) (图片看起来太小,您可以按Ctrl +鼠标滚轮放大/缩小)

在此处输入图片说明

It looks like you're using ajax to get the string, and just setting the dataType to html will automatically parse it for you. 看起来您正在使用ajax来获取字符串,并且只需将dataType设置为html即可自动为您解析。
To find an element within that data, you use regular DOM traversal methods: 要在该数据中查找元素,请使用常规的DOM遍历方法:

$.ajax({
    url : 'somewhere.php',
    dataType : 'html'
}).done(onS);

function onS(data) {
     var node = $(data).find('#div'); // or filter('#div') for root elements

     alert("Data Loaded: " + node.html());
}

if for some reason you just have to iterate, it looks like you're looking for either the tagName or the id ? 如果由于某种原因您只需要进行迭代,看起来您正在寻找tagName还是id?

$.each(html, function (i, ele) {
    ele.tagName // returns 'div', 'span' etc. often in uppercase
    ele.id // would return the id, 
           // and from the hashsign it looks like it's what you're trying to do

    ....
});

You don't have to loop through it all. 您不必遍历所有内容。 You can use jQuery selectors as usual: 您可以照常使用jQuery选择器:

var theNodeYoureLookingFor = $('#output', html);

Here's a silly little fiddle to demonstrate it: http://jsfiddle.net/RjAMw/1/ 这是一个愚蠢的小提琴来演示它: http : //jsfiddle.net/RjAMw/1/

EDIT: Update in response to question update: 编辑:更新以响应问题更新:

Your onS function takes the fetched data as argument. 您的onS函数将获取的data作为参数。 data is already in HTML format, so all you need to do is: data已经是HTML格式,因此您需要做的是:

var $node = $('div#output', data);
alert('Data Loaded: ' + $node.html());

From what I can see, the errors you've been given is because you're trying to access a data.results array, which isn't there. 从我所看到的,您得到的错误是因为您正在尝试访问不存在的data.results数组。

I finally figured it out. 我终于弄明白了。

The reason why the div content is empty is because that #output div will be generated 2-4 seconds later after the html document is loaded. div内容为空的原因是因为#output div将在加载html文档后的2-4秒后生成。 So when the onS() method is triggered the content of that div hasn't been generated. 因此,在触发onS()方法时,尚未生成该div的内容。

As to how to retrieve the generated/dynamic/delayed content of a div from a cross domain website, please refer to this question: 关于如何从跨域网站检索div的生成/动态/延迟内容,请参考以下问题:

how to use JavaScript(jQuery etc.) to get delayed/generated/dynamic content from an external website page 如何使用JavaScript(jQuery等)从外部网站页面获取延迟/生成/动态内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM