对图像应用 window.hostname href 吗?

发布于 2024-12-12 20:08:14 字数 387 浏览 4 评论 0原文

如果图像 html 是

<img src="img1" class="homeimage"/>

如何使用 jquery 将 包裹在图像周围,并使用 window.hostname 作为 href ?

因此,如果页面的 url 是 http://www.google.com/search?jksjkjskaj

jquery 应该返回

<a href="http://www.google.com"><img src="img1" class="homeimage"/></a>

if an image html is

<img src="img1" class="homeimage"/>

how using jquery would you wrap a <a> around the image with window.hostname as a href?

so if the url of a page is http://www.google.com/search?jksjkjskaj

the jquery should return

<a href="http://www.google.com"><img src="img1" class="homeimage"/></a>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薄荷港 2024-12-19 20:08:14

查看 jQuery 的 .wrap()

$(".homeimage").wrap("<a href='" + location.protocol + '//' + location.hostname + "'></a>");

示例: http://jsfiddle.net/CRN86/1/

Check out jQuery's .wrap()

$(".homeimage").wrap("<a href='" + location.protocol + '//' + location.hostname + "'></a>");

Example: http://jsfiddle.net/CRN86/1/

去了角落 2024-12-19 20:08:14
$('.homeimage').wrap('<a href="' + location.protocol + '//' + location.hostname + '"></a>');

应该有效。

$('.homeimage').wrap('<a href="' + location.protocol + '//' + location.hostname + '"></a>');

should work.

岁月染过的梦 2024-12-19 20:08:14

除了上面提出的解决方案之外,我想添加另一个解决方案:

$('.homeimage').wrap('<a href="' + location.origin + '"></a>');

Instead the solutions proposed above, I want to add another one:

$('.homeimage').wrap('<a href="' + location.origin + '"></a>');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文