JQuery //删除Id字符

时间:2017-04-06 16:40:52

标签: jquery replace attributes

尝试从Id attr。

中删除主题标签

HTML:

<div id="#First">1</div>
<div id="#Second">2</div>
<div id="#Third">3</div>

JQuery的:

$('[id^="#"]').attr('id').replace("#", "");

2 个答案:

答案 0 :(得分:1)

You can try it with the following jQuery snippet. I just changed a bit of your provided code.

$('[id^="#"]').each(function(){ // Loop through all selected divs
    this.id = this.id.replace('#', '');  // Replace Id
});

Quick demo:

The ID's get replaced and the CSS changes the color of the right ID's to green.

$('[id^="#"]').each(function(){ // Loop through all inputs
    this.id = this.id.replace('#', '');  // Replace ID
});
#First {
  color: green;
  font-weight:bold;
}

#Second {
  color: green;
  font-weight:bold;
}

#Third {
  color: green;
  font-weight:bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="#First">1</div>
<div id="#Second">2</div>
<div id="#Third">3</div>

答案 1 :(得分:0)

Your question has become vague, I do not know exactly what you need, but this way you can get the ids individually without "#".

$( window ).on("load",
    function() {

        $("body").find('[id^="#"]').each(
            function(){
                var id = $(this).attr('id').replace("#", "");
                console.log(id);
                $(this).attr("id", id); // To change use this.
            }
        );      
    }
);