javascript - jQuery: Get inner HTML from a HTML code within a variable - Stack Overflow

I have a variable which contains a html element:alert(aData[3]);gives me:BLA BLA BLA<div style=&quo

I have a variable which contains a html element:

alert(aData[3]);

gives me:

BLA BLA BLA
<div style="display: none">
 <table>....</table>
</div>

I'm trying to figure out how to get the contents WITHIN the div-tag from the variable aData[3] via jQuery. I tried various things:

elem = $(aData[3]).find("div");
alert(elem.html());

// OR
elem = $(aData[3]).find("div");
alert(elem[0].html());

// OR
elem = $(aData[3]).find("div");
alert($(elem[0]).html());

// OR
elem = $(aData[3], 'div');
alert(elem.html());

Can't get any of those to work. How to the correct version? :( Thanks a lot

I have a variable which contains a html element:

alert(aData[3]);

gives me:

BLA BLA BLA
<div style="display: none">
 <table>....</table>
</div>

I'm trying to figure out how to get the contents WITHIN the div-tag from the variable aData[3] via jQuery. I tried various things:

elem = $(aData[3]).find("div");
alert(elem.html());

// OR
elem = $(aData[3]).find("div");
alert(elem[0].html());

// OR
elem = $(aData[3]).find("div");
alert($(elem[0]).html());

// OR
elem = $(aData[3], 'div');
alert(elem.html());

Can't get any of those to work. How to the correct version? :( Thanks a lot

Share Improve this question asked Apr 9, 2014 at 8:19 timtim 10.1k22 gold badges87 silver badges146 bronze badges 10
  • 1 @DejaVu There are no styles to follow so it doesn't matter. – George Commented Apr 9, 2014 at 8:21
  • 1 @tim He means, quite rightly, that styles should be separated by a semi-colon. Although the last property in the block doesn't need one. More here – George Commented Apr 9, 2014 at 8:25
  • 1 @tim — He is suggesting that you use ; as a rule terminator in your CSS instead of as a rule separator. It has no bearing on your actual problem. – Quentin Commented Apr 9, 2014 at 8:26
  • 1 Ah I see, sure I know that, but for one property, it's not necessary, is it? And yes, actually no relation to my problem in any way... – tim Commented Apr 9, 2014 at 8:27
  • 1 @tim That's right, the issue is irrelevant. – George Commented Apr 9, 2014 at 8:28
 |  Show 5 more ments

3 Answers 3

Reset to default 4

find looks for descendants of elements in the jQuery object, but div is the highest level element in your HTML (it isn't a descendant of anything).

Just don't use find.

elem = $(aData[3]);
alert(elem.html());

You need to wrap your string with another dom object to use .find().

elem = $('<div />',{
  html: aData[3];
}).find("div");
alert(elem);

You can use parseHTML to convert the string into an array of DOM elements.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745326501a4622680.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信