如果<h1>
不存在,则查找文档中的第一个标题标签(<h2>
通过之一<h6>
),如果标签文本等于标题文本,则将元素更改为<h1 class="heading1">
.
据我所知,这是可行的,但必须有一种更有效的方式来编写它。
var titleText = $('title').html()
var hOne = $('h1:eq(0)');
var hTwo = $('h2:eq(0)');
var hThree = $('h3:eq(0)');
var hFour = $('h4:eq(0)');
if (hOne.html() == titleText)
{
return;
}
else if (hTwo.html() == titleText)
{
var hTwoText = hTwo.html();
hTwo.replaceWith(function () {
return '<h1 class="heading1">' + hTwoText + "</h1>";
});
}
else if (hThree.html() == titleText)
{
var hThreeText = hThree.html();
hThree.replaceWith(function () {
return '<h1 class="heading1">' + hThreeText + "</h1>";
});
}
else if (hFour.html() == titleText)
{
var hFourText = hFour.html();
hFour.replaceWith(function () {
return '<h1 class="heading1">' + hFourText + "</h1>";
});
}