>我需要用类似href"https://www.google.com/favicon.ico"
的东西替换 href
<link rel="shortcut icon" href="https://s.ytimg.com/yts/img/favicon-vfl8qSV2F.ico" type="image/x-icon">
如何使用 jquery/javascript 字符串执行此操作?
到目前为止,我的建议: 这应该适用于标签,但似乎不适用于<link>
标签。
$("link[href='https://s.ytimg.com/yts/img/favicon-vfl8qSV2F.ico']").attr('href', 'https://www.google.com/favicon.ico')
我正在寻找一种快速方法来替换网页上的图标,而无需在控制台中执行 javascript 字符串。
link
元素可能没有href
属性中快捷方式图标的完整路径,它可能是相对路径。搜索属性会精确搜索属性中的该文本。
我会寻找rel
属性:
$("link[rel='shortcut icon']").attr("href", "https://www.google.com/favicon.ico");
或者您可以使用属性选择器的"结尾为"形式(更多在规范中(:
$("link[href$='/favicon-vfl8qSV2F.ico']").attr("href", "https://www.google.com/favicon.ico");
。但我会使用rel
.
您已经说过要向页面添加jQuery来执行此操作。没有必要,改用 DOM:
document.querySelector("link[rel='shortcut icon']").href = "https://www.google.com/favicon.ico";