jQuery Selection无法选择所有元素

时间:2017-04-20 05:36:21

标签: javascript jquery

所以我基本上创建了一个快速的小提取器。我希望能够提取数据。我能够选择所有强大的元素。但出于某种原因,我去选择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);
})

1 个答案:

答案 0 :(得分:2)

因为您已将DOM方法绑定到jquery对象。而是使用它:

el[0].innerHTML = h;
// el.html(h); // <--or this one

示例:

&#13;
&#13;
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;
&#13;
&#13;