如何从包含在<a> tag, using a query selector?

时间:2017-06-14 16:28:02

标签: javascript html dom

I want to remove the textDecoration from a special item using a javascript ( preferably a query selector. I don't know what I'm doing wrong though. Here's the code. CodePen

中的列表项中删除文本修饰
<h1>Thanks for the Help!</h1>
  <h1> My problem set </h1>
  <ul>
    <li id="highlight">List Item 1</li>
    <li class="bolded">List Item 2</li>
    <a href="#" class="special"><li class="bolded">List Item 3</li></a>
  </ul>

var sLi = document.querySelector("ul a.special");
for (var i = 0; i <= sLi.length; i++){
  sLi[i].style.textDecoration = "none";
}

1 个答案:

答案 0 :(得分:1)

没有必要循环查询结果,因为querySelector()只返回一个项目。

如果你一直在使用querySelectorAll(),你会希望循环返回一个节点列表。

var sLi = document.querySelector("ul a.special");
sLi.style.textDecoration = "none";
<h1>Thanks for the Help!</h1>
  <h1> My problem set </h1>
  <ul>
    <li id="highlight">List Item 1</li>
    <li class="bolded">List Item 2</li>
    <li class="bolded"><a href="#" class="special">List Item 3</a></li>
  </ul>

另外(仅供参考),li应该包含a,而不是相反。