Jquery:获取完整路径的图标

编程入门 行业动态 更新时间:2024-10-25 06:21:03
本文介绍了Jquery:获取完整路径的图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想用Jquery从网站上抓取图标的链接。我简单地使用 $('link [rel =快捷方式图标]')。attr('href'); ,它工作正常。

但是......我并不总是有一条完整的路径。例如,我可以拥有类似于

mywebsite/myicon.ico 或者只有一个相对路径

/myicon.ico

我希望始终拥有完整路径,即使Url编码为一个相对路径。是否有简单的解决方案?

我将它用作Google Chrome扩展程序中的内容脚本。

'DIV CLASS = h2_lin>解决方案

如果您使用 HREF节点属性,你应该总是得到一个绝对路径:

var url = $('link [rel =shortcut icon]')[0] .href;

或者:

var url = $('link [rel =shortcut icon]')。prop('href');

或者,在普通JavaScript中:

var url = document.querySelector('link [rel =shortcut icon]')。href;

I want to grab the link of a favicon from a website with Jquery. I simply use $('link[rel="shortcut icon"]').attr('href'); and it works fine.

But... I do not always have a complete path. For example I can have something like

mywebsite/myicon.ico

or only a relative path

/myicon.ico

I would like to always have the full path, even if the Url is coded as a relative path. Is there a simple solution to this?

I use this as a Content Script in a Google Chrome Extension.

解决方案

if you use the href property of the node, you should always get an absolute path:

var url = $('link[rel="shortcut icon"]')[0].href;

Or:

var url = $('link[rel="shortcut icon"]').prop('href');

Or, in plain JavaScript:

var url = document.querySelector('link[rel="shortcut icon"]').href;

更多推荐

Jquery:获取完整路径的图标

本文发布于:2023-11-10 02:04:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574049.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图标   路径   完整   Jquery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!