简体   繁体   English

如何将不同的 css class 选择器组合成一条 javascript 线?

[英]How to combine different css class selectors into one javascript line?

How can this code be simplified into one line (js statement)?这段代码如何简化为一行(js语句)?

     <script>$(".div2").click(function() {window.location = $(this).find("a").attr("href"); return false;});</script>
     <script>$(".div3").click(function() {window.location = $(this).find("a").attr("href"); return false;});</script>
     <script>$(".div4").click(function() {window.location = $(this).find("a").attr("href"); return false;});</script>
     <script>$(".div5").click(function() {window.location = $(this).find("a").attr("href"); return false;});</script>
     <script>$(".div6").click(function() {window.location = $(this).find("a").attr("href"); return false;});</script>
     <script>$(".div7").click(function() {window.location = $(this).find("a").attr("href"); return false;});</script>
     <script>$(".div8").click(function() {window.location = $(this).find("a").attr("href"); return false;});</script>
     <script>$(".div9").click(function() {window.location = $(this).find("a").attr("href"); return false;});</script>

You can short it down to something like this:您可以将其缩短为以下内容:

$(".div2,.div3,.div4,.div5,.div6,.div7,.div8,.div9").click(function() {
  window.location = $(this).find("a").attr("href");
  return false;
});

Also there is no need to use multiple <script> tags, 1 is enought.也不需要使用多个<script>标签,1 就足够了。

Two answers:两个答案:

Selector group选择器组

You can use a selector group :您可以使用选择器组

$(".div2, .div3, .div4, .div5, .div6, .div7, .div8, .div9")..click(function() {
    window.location = $(this).find("a").attr("href");
    return false;
});

A selector group is a series of selectors with commas in-between.选择器组是一系列中间有逗号的选择器。 It selects elements matching any of the selectors in the group.它选择与组中任何选择器匹配的元素。

Event delegation事件委托

Rather than adding a handler to all of those divs, you could add one to document that only takes action if the click passed through one of those divs:您可以向document中添加一个处理程序,而不是向所有这些 div 添加一个处理程序,该处理程序仅在单击通过这些 div 之一时才会执行:

$(document).on("click", ".div2, .div3, .div4, .div5, .div6, .div7, .div8, .div9", function() {
    window.location = $(this).find("a").attr("href");
    return false;
});

Even if you had multiple statements, there's no reason you couldn't put them in a single script :即使您有多个语句,也没有理由不能将它们放在一个script中:

<script>
doThis();
doThat();
doTheOther();
</script>

add a common class to elements, as follows.给元素添加一个普通的class,如下。

<div class="div2 my-class"> ... </div>
<div class="div3 my-class"> ... </div>
<div class="div3 my-class"> ... </div>
<div class="div4 my-class"> ... </div>
<div class="div5 my-class"> ... </div>
<div class="div6 my-class"> ... </div>
<div class="div7 my-class"> ... </div>
<div class="div8 my-class"> ... </div>

add the event listener to that common class将事件侦听器添加到该公共 class

$(".my-class").click(function() {
  window.location = $(this).find("a").attr("href");
  return false;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM