简体   繁体   English

点击更改ID

[英]Change an ID upon click

I know how to change a class name with another class upon click, but not sure how to do so when using id instead. 我知道如何在单击时更改另一个班级的班级名称,但不确定使用id时该如何更改。

Below is how the class is setup: 下面是该类的设置方式:

<script>

    $(function(){
      $(".collapseArrow").click(function(){
        $(".collapseArrow").removeClass("collapseArrow")
        $(this).addClass("collapseArrowUp")
        return false;
      })
    })
    </script>

The reason for that is that the class is already being used and i have to use the id to style it instead. 这样做的原因是该类已经被使用,我不得不使用id来设置其样式。

Plain Javascript approach:- 普通的Javascript方法:-

This approach takes advantage of the this keyword as follows... 这种方法利用了this关键字,如下所示...

HTML: HTML:

<div id="oldId" onclick="changeId(this)"></div>

JS: JS:

function changeId(element) {
    element.id = "someNewId";
}

or simply (in one line) as part of the HTML 或简单地(一行)作为HTML的一部分

<div id="oldId" onclick="this.id='someNewId';"></div>

jQuery approach:- jQuery方法:-

Use the attr() function as follows... 如下使用attr()函数...

$(function(){
     $("#oldId").click(function(){
       $("#oldId").attr("id","newId");
        return false;
     });
});

EDIT: As requested, I will give a piece of code to toggle between 2 ids. 编辑:根据要求,我将提供一段代码在2个ID之间切换。

HTML: HTML:

<div id="firstId" onclick="toggleId(this)"></div>

JS: JS:

function toggleId(element) {
    if(element.id == "firstId") {
        element.id = "secondId";
    } else { //This will only execute when element.id == "secondId"
        element.id = "firstId";
    }
}

You can use the attr() function 您可以使用attr()函数

<script>
  $(function(){
     $("#collapseArrow").click(function(){
       $("#collapseArrow").attr('id',"collapseArrowUp");
        return false;
     });
  });
</script>

You can use 您可以使用

 $(this).attr("id"',"new id");

But i would use classes and add important to override existing css rules 但是我会使用类并添加重要内容以覆盖现有的CSS规则

Hope i helped 希望我有所帮助

Instead of changing the id, do it with multiple classes. 无需更改id,而是对多个类进行更改。 This could help you... 这可以帮助您...

$(function(){
  $(".collapseArrow").click(function(){
    $(".collapseArrow").removeClass("up")
    $(this).addClass("up")
    return false;
  })
})

i have to use the id to style it instead. 我必须使用ID来设置其样式。

I suggest you not to do this because in the dom structure id s have most preference against the class names. 我建议您不要这样做,因为在dom结构中id优先于类名。 So this would be little difficult to override the styles which have been applied with the ids. 因此,覆盖id所应用的样式几乎没有什么困难。

Instead i would recommend to use classes instead: 相反,我建议改用类:

$(function() {
  $(".collapseArrow").click(function() {
    $(this).toggleClass("collapseArrow collapseArrowUp")
    return false;
  });
});

 $(function() { $(".collapseArrow").click(function() { $(this).toggleClass("collapseArrow collapseArrowUp") return false; }); }); 
 .collapseArrow::before { content: "[-]" } .collapseArrowUp::before{ content: "[+]" } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h1 class='collapseArrow'></h1> 

You should not change an id. 您不应该更改ID。 If you need to use a different selector to select the same element, add another class to the element, eg 如果需要使用其他选择器来选择相同的元素,请向该元素添加另一个类,例如

<span class="collapsArrow SomethingMeaningful"></span>

Then you just need to use a different selector method: 然后,您只需要使用其他选择器方法:

$( "span[class~='SomethingMeaningful']" )
    .removeClass("collapseArrow")
    .addClass("collapseArrowUp");

Then you can style the element with: 然后,您可以使用以下样式设置元素的样式:

.SomethingMeaningful{

}

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

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