我正在尝试使用 jQuery 从 div id 中删除最后一个字符。目前我有:
<div id="foo/"></div>
但我需要:
<div id="foo"></div>
该字符始终是 /
并且由我正在使用的 CMS 自动生成(re:annoyingly)。有了 /
搞乱了我正在尝试做的一些 JavaScript 链接。
如何解决这个问题?
最佳答案
使用 attribute$=
CSS3 选择器很容易,这意味着在 jQuery 选择中“结束于”:
$("div[id$=/]").each(function(){
this.id = this.id.substr(0,this.id.length - 1);
});
编辑:使用 Adobe® 浏览器实验室,我在 IE6、IE7、Firefox 2.0 和 Firefox 3.0、Safari 3.0 中测试了此代码。我还在 Safari 4.0 和 Firefox 3.5 中进行了本地测试。在所有浏览器中它都能正常工作,并从 id 中删除了尾随的 /
。
关于jquery - 删除 id 属性中的最后一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1794822/