试图解析标签元素

时间:2015-09-11 13:05:11

标签: javascript html

我正在尝试使用JavaScript定位列表<li>解析网页而不使用类。

<ul id="cartItems">
  <li class="heading">
  <li>...</li>
  <li>...</li>
  <li>...</li>
  <li>...</li>
</ul>

2 个答案:

答案 0 :(得分:0)

使用querySelectorAll获取没有课程的所有li元素。

var liWithoutClass = document.querySelectorAll('#cartItems > li:not([class])');

console.log(liWithoutClass);

document.write(liWithoutClass[0].textContent); // Output the first li
document.write('<br />' + liWithoutClass[3].textContent); // Output the last li
<ul id="cartItems">
  <li class="heading">Heading</li>
  <li>Element 1</li>
  <li>Element 2</li>
  <li>Element 3</li>
  <li>Element 4</li>
</ul>

答案 1 :(得分:0)

&#13;
&#13;
    var elems = document.querySelector(".heading");
    console.log(elems);
//Now we strip the class out like this:
    elems.setAttribute(class, "none");
//Now we display the updated values below
console.log(elems);  
// This won't work in Stack's editor, so you'll have to validate it against the page you want.
//Now, just grab the entire element by id from the page as below:

var updatedContent = document.getElementById("cartItems");
console.log(updatedContent);
//Again, stack's editor is limited, but overall this should work fine on your page.
&#13;
    <ul id="cartItems">
      <li class="heading">
      <li>item 1</li>
      <li>item 2</li>
      <li>item 3</li>
      <li>item 4</li>
    </ul>
&#13;
&#13;
&#13;