所以我基本上创建了一个快速的小提取器。我希望能够提取数据。我能够选择所有强大的元素。但出于某种原因,我去选择p元素。我没有得到任何选择。
见Js Fiddle:https://jsfiddle.net/xmikedanielsx/e34he23d/1/
var h = `<h4>
<a id="A" name="A"> </a>A</h4>
<p>
<a id="Abrupt" name="Abrupt"> </a>
<strong>Abrupt Climate Change</strong>
<br/> Sudden (on the order of decades), large changes in some major component of the climate system, with rapid, widespread effects.</p>
<p>
<a id="Adaptation" name="Adaptation"> </a>
<strong>Adaptation</strong>
<br/> Adjustment or preparation of natural or human systems to a new or changing environment which moderates harm or exploits beneficial opportunities.</p>`
var el = $('<div></div>');
el.innerHTML = h;
$('strong', el.innerHTML).each(function() {
alert($(this)["0"].innerHTML);
})
$('p', el.innerHTML).each(function() {
alert($(this)["0"].innerHTML);
})
答案 0 :(得分:2)
因为您已将DOM方法绑定到jquery对象。而是使用它:
el[0].innerHTML = h;
// el.html(h); // <--or this one
示例:强>
var h = '<h4><a id="A" name="A"> </a>A</h4><p> <a id="Abrupt" name="Abrupt"> </a><strong>Abrupt Climate Change</strong><br/>Sudden (on the order of decades), large changes in some major component of the climate system, with rapid, widespread effects.</p><p><a id="Adaptation" name="Adaptation"> </a><strong>Adaptation</strong><br/>Adjustment or preparation of natural or human systems to a new or changing environment which moderates harm or exploits beneficial opportunities.</p>'
var el = $('<div></div>');
el.html(h);
$('strong', el).each(function() {
alert($(this)["0"].innerHTML);
});
$('p', el).each(function() {
alert($(this)["0"].innerHTML);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;