使用 JavaScript 从其内容设置标题 ID 并将空格换成连字符并变为小写的最简单方法是什么。
例如,如果标题是:
<h1>Header one content</h1>
我们如何将其更改为:
<h1 id="header-one-content">Header one content</h1>
谢谢
最佳答案
另一种方式,遍历所有H1标签并执行操作:
$("h1").each(function() {
var hyphenated = $(this).text().replace(/\s/g,'-');
$(this).attr('id',hyphenated);
}
);
关于javascript - 使用 JavaScript 从内容设置 header id 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4146306/