JQuery // 删除 ID 字符



正在尝试从 Id 属性中删除主题标签。

目录:

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

JQuery:

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

您可以使用以下 jQuery 代码段进行尝试。我刚刚更改了一些您提供的代码。

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

快速演示:

ID

被替换,CSS 将正确 ID 的颜色更改为绿色。

$('[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>

你的问题变得模糊了,我不知道你到底需要什么,但是这样你可以单独获取id,没有"#"。

$( 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.
            }
        );      
    }
);

最新更新