【问题标题】:How do I add an underline for all the links on the website when I click the button?单击按钮时,如何为网站上的所有链接添加下划线?
【发布时间】:2020-09-13 14:52:18
【问题描述】:

在创建一个简单的脚本经过一个小时的反复试验后,我向您寻求帮助。我想创建一个button,单击它后会为网站上的所有a 选择器添加下划线样式。我已经写了一个简单的函数,但不幸的是它不起作用。 整页有大量a选择器,整页代码我就不发了。

JS 文件:

function underlineLinks() {
  const links = document.querySelectorAll("a");
  links.style.textDecoration = "underline";
}

HTML 代码:

<button onclick="underlineLinks()">Underline</button>

functions.php 文件中的 PHP 代码:

function underline_links() {
  wp_enqueue_script( 'js-file', get_stylesheet_directory_uri() . '/js/underline.js');
}
add_action('wp_enqueue_scripts', 'underline_links');

【问题讨论】:

  • querySelectorAll 返回一个类似结果的数组。您必须遍历它们才能访问它们的每一个 style 属性并单独设置它们。
  • &lt;body&gt; 添加一个类并使用CSS 规则会容易得多。

标签: javascript html wordpress


【解决方案1】:

并没有真正解释手头问题的原因,但这里已经有足够多的了。

对整个文档或正文使用 css 似乎比单独循环遍历每个元素更简单。

window.onload = function(){
  document.getElementById('on').onclick = function(){
    document.body.classList.add('underline');
  };
  
  document.getElementById('off').onclick = function(){
    document.body.classList.remove('underline');
  }
}
a{text-decoration: none}
body.underline a{text-decoration: underline}
<a>link 1</a>
<a>link 2</a>
<a>link 3</a>

<button id = 'on'>on</button>
<button id = 'off'>off</button>

一般来说,我会尽量避免使用 HTMLElements 的 style 属性,而是尝试使用类/属性。

按要求编辑:

a{text-decoration: none}
body.underline a{text-decoration: underline}
<a>link 1</a>
<a>link 2</a>
<a>link 3</a>

<button onclick = "document.body.classList.toggle('underline')">toggle</button>

【讨论】:

  • 好的,谢谢您的帮助。如果设计中必须要有一个按钮会怎样?
  • 使用document.body.classList.toggle('underline')
【解决方案2】:

您正在尝试将内联样式添加到从 .querySelectorAll() 返回的链接集合中。但是该集合没有style 属性。 而是在单击按钮时循环遍历集合中的所有链接并切换 CSS 类的使用。

// When the button is clicked...
document.querySelector("button").addEventListener("click", function(){
  // Find all the <a> elements and loop over the collection of them
  document.querySelectorAll("a").forEach(function(link){
    // Instead of working with inline styles (the style property),
    // just add or toggle the use of a pre-made CSS class. I'm using
    // toggle here, but if you just want the class added use .add("underline")
    // instead.
    link.classList.toggle("underline");
  });
});
a { text-decoration:none; }
.underline { text-decoration:underline; }
<a href="someURL">the text of the link</a> lorem ipsum <a href="someURL">the text of the link</a> lorem ipsum 2<a href="someURL">the text of the link</a> lorem ipsum 
<a href="someURL">the text of the link</a> lorem ipsum <a href="someURL">the text of the link</a> lorem ipsum <a href="someURL">the text of the link</a> lorem ipsum 
<a href="someURL">the text of the link</a> lorem ipsum 
<div>
  <button>Click to toggle underline</button>
</div>

【讨论】:

  • 好的,如果我希望选择器“a”的样式用作内联样式怎么办?
  • @nsog8sm43x 然后使用 link.style.textDecoration = "underline"; 的原始内联代码,但除非您有特定的需要,否则不建议这样做。
猜你喜欢
  • 2015-10-29
  • 1970-01-01
  • 1970-01-01
  • 2013-04-29
  • 1970-01-01
  • 2012-04-19
  • 2022-12-29
  • 1970-01-01
相关资源
最近更新 更多