|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
比如这样设置的a标签他会提示:链接必须具有可识别的文本:元素没有标题属性影响资源
html
- <div id="a" class="efsef">
- <div class="1"></div>
- <div class="2"></div>
- <div class="3"></div>
- <div class="4"></div>
- </div>
复制代码
body
- changeTagById("a", "a", "网站名")
复制代码
js
- /**
- * 修改指定id的标签
- * @param {any} idname - 指定id
- * @param {any} tagname - 修改的标签名,默认为"div"
- * @param {any} adds - 如果是a标签或者img标签,需要填入路径,默认为"#"
- */
- function changeTagById(idname, tagname = "div", adds = "#") {
- let idnames = document.getElementById(`${idname}`)
- let tagnames = document.createElement(`${tagname}`)
- if (idnames) {
- tagnames.setAttribute("id", idname + "1")
- let idnamesss = idname + "1"
- tagnames.setAttribute("class", idnames.className)
- idnames.after(tagnames)
- let newidname = document.getElementById(`${idnamesss}`)
- let lenthsss = idnames.children.length
- for (let i = 0; i < lenthsss; i++) {
- newidname.append(idnames.children[0])
- }
- newidname.setAttribute("id", idname)
- idnames.remove()
- //newidname.setAttribute("is", `${tagname}`)
- if (tagname == "a") {
- newidname.setAttribute("href", adds)
- }
- return true
- } else {
- checkid(`${idname}`)
- return false
- }
- }
复制代码
您好,自定义标签是一种扩展HTML元素的方式,可以用来创建自己的标签。然而,如果您使用了与内置标签相同的名称,浏览器将会忽略您的自定义标签。因此,在上述代码中,如果您将标签名设置为"a",则不会生效。
要解决这个问题,您可以选择修改自定义标签的名称,或者使用某种形式的标记来标识您的自定义标签。以下是修改自定义标签名称的示例代码:
- <!DOCTYPE html>
- <html>
- <body>
- <mytag id="myTag">自定义标签</mytag>
- <script>
- function changeTagById(idname, tagname = "div", adds = "#") {
- let idnames = document.getElementById(idname);
- if (idnames) {
- let tagnames = document.createElement(tagname);
- tagnames.setAttribute("id", idname + "1");
- tagnames.setAttribute("class", idnames.className);
- idnames.after(tagnames);
- let newidname = document.getElementById(idname + "1");
- let childrenCount = idnames.children.length;
- for (let i = 0; i < childrenCount; i++) {
- newidname.append(idnames.children);
- }
- newidname.setAttribute("id", idname);
- idnames.remove();
- if (tagname == "mytag") {
- newidname.textContent = document.getElementById(idname).textContent;
- }
- return true;
- } else {
- checkid(idname);
- return false;
- }
- }
- changeTagById("myTag", "mytag");
- </script>
- </body>
- </html>
复制代码
在以上示例中,我们将自定义标签的名称从"a"改为"mytag",并将JavaScript代码中的相关部分进行了相应的修改。
希望这可以帮助到您!如果您有任何其他问题,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
|