简体   繁体   English

使用jQuery切换对象的类

[英]Toggling the class of an object with jquery

I have elements that are added by the user (when they click a button), it adds ap and a pre element. 我有用户添加的元素(当他们单击按钮时),它添加了ap和pre元素。 The pre element is invisible and has no class while the p element has a class of "query-p". pre元素是不可见的,没有任何类,而p元素具有“ query-p”的类。

What I'm trying to do is to make it so whenever the user clicks on ap element with "data-id='p1'", it add the class "show" to the pre element with "data-id='pre1'" or for example when you click on ap element with "data-id='p2'", it add the class "show" to the pre element with "data-id='pre2'" and so on. 我想做的就是做到这一点,以便每当用户单击带有“ data-id ='p1'”的ap元素时,它将通过“ data-id ='pre1'”将类“ show”添加到pre元素中”,例如,当您单击带有“ data-id ='p2'”的ap元素时,它将通过“ data-id ='pre2'”将类“ show”添加到pre元素中,依此类推。

This is my jquery code : 这是我的jQuery代码:

$(function () {
    $("p[data-id]").on("click", function() {
    var idFound = $(this).data("id");

            if ($("p[data-id]").attr("class") == "query-p" && $("pre[data-id]").attr("class") != "show") {
                        $("pre[data-id]").attr("class", "show");
            }
            else if ($("pre[data-id]").attr("class") == "show") {
                        $("pre[data-id]").removeAttr("class");
            }
    });
});

This is my HTML (the elements that I'm working with are not in this code, I put it here because it might help): https://pastebin.com/eKVbUZHQ 这是我的HTML(我正在使用的元素不在此代码中,我将其放在此处,因为它可能会有所帮助): https : //pastebin.com/eKVbUZHQ

This is my other javascript file (it mostly contains the code that adds the elements that I'm working with) : https://pastebin.com/yEZuuhA8 这是我的另一个javascript文件(它主要包含添加我正在使用的元素的代码): https : //pastebin.com/yEZuuhA8

The problem that I'm having is that my code shows all pre elements instead of only the one it's supposed to. 我遇到的问题是我的代码显示了所有前置元素,而不是只显示了应该包含的元素。

EXAMPLE : 范例:
I added new elements with : 我添加了新的元素:

p element 1 : id="display-pre1" class="query-p" data-id="p1" pre element 1 : id="display-pre-a1" data-id="pre1" p元素1:id =“ display-pre1” class =“ query-p” data-id =“ p1” pre元素1:id =“ display-pre-a1” data-id =“ pre1”

p element 2 : id="display-pre2" class="query-p" data-id="p2" pre element 2 : id="display-pre-a2" data-id="pre2" p元素2:id =“ display-pre2” class =“ query-p” data-id =“ p2” pre元素2:id =“ display-pre-a2” data-id =“ pre2”

The pre elements are hidden with "display: 'none'". 前置元素用“ display:'none'”隐藏。 All elements with class "show" have "display: 'block'". 所有具有“ show”类的元素都具有“ display:'block'”。 The pre elements have no class. 前置元素没有类。

Now, whenever I click on the first p element, it adds the class "show" to both the pre element 1 and the pre element 2, so they both get "display: 'block'", when I click it again it hides both of the pre elements again. 现在,每当我单击第一个p元素时,它都会在pre元素1和pre元素2中都添加“ show”类,因此它们都将获得“ display:'block'”,当我再次单击它时,它们都将隐藏的前元素再次。

Hope this helps a bit. 希望这个对你有帮助。

Some of the issues within the click handler: 点击处理程序中的一些问题:

  • With $("p[data-id]") you select all p elements with a data-id attribute, while you need to only select the clicked one. 使用$("p[data-id]")您可以选择所有具有data-id属性的p元素,而只需选择被单击的元素。
  • With $("pre[data-id]") you select all pre elements with a data-id attribute, while you need to only select one with a particular value for that attribute. 使用$("pre[data-id]")您可以选择所有具有data-id属性的pre元素,而只需要为该属性选择一个具有特定值的元素。
  • You compare the class attribute with "query-p" , but then why not put this condition in a way that the click handler is only defined for those? 您将class属性与"query-p" ,但是为什么不以仅为那些定义点击处理程序的方式放置此条件呢? Then you don't have to check this anymore once the user has clicked. 这样,用户单击后就不必再检查了。
  • The code with attr("class") and removeAttr("class") assumes that an element will have at the most one CSS class. 带有attr("class")removeAttr("class")假定一个元素最多具有一个CSS类。 This is restricting the possibilities. 这限制了可能性。 Elements should be allowed to have multiple CSS classes defined for it. 应该允许元素具有为其定义的多个CSS类。

Here is a small snippet to demo how it could work: 以下是演示其工作方式的一小段代码:

 $(function () { $("p.query-p[data-id]").on("click", function() { var data = $(this).data("id").replace(/^p/, "pre"), $pre = $("pre[data-id=" + data + "]"); $pre.toggleClass("show"); }); }); 
 pre { display: none } pre.show { display: block } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p class="query-p" data-id="p1">para 1</p> <p class="query-p" data-id="p2">para 2</p> <pre data-id="pre1">pre 1</pre> <pre data-id="pre2">pre 2</pre> 

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

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