动态包装图像与锚href。

时间:2021-10-16 07:29:33

For all img elements with the class border, I want to wrap them with an anchor element that uses Blogger's data:post.link variable to link to a specific post.

对于具有类边界的所有img元素,我希望用使用Blogger的数据的锚元素:post来包装它们。链接变量以链接到特定的帖子。

I'm using some code that I found here, but it's not having any effect:

我正在使用一些我在这里找到的代码,但它没有任何效果:

jQuery("img.border").wrap("<a expr:href='data:post.link'></a>");

Any help would be appreciated!

任何帮助都将被感激!

EDIT: It's now adding the anchor, but it's not reading the expr:href properly, so I get an empty anchor with no attributes.

编辑:它现在正在添加锚点,但是它没有正确地读取expr:href,所以我得到一个没有属性的空锚点。

2 个解决方案

#1


1  

var aNode = $('<a>');
aNode.attr('expr:href', 'data:post.link');
$('img.border').wrap(aNode);

#2


2  

This is working for me:

这对我很有效:

function wrapHandler() {
  return "<a expr:href='data:post.link'></a>";
}

$("img.border").wrap(wrapHandler);

I viewed the DOM after calling wrapHandler and it does add the correct wrapper. You can see it working here: https://jsfiddle.net/hyygm9vv/

我在调用wrapHandler后查看了DOM,它确实添加了正确的包装器。您可以看到它在这里工作:https://jsfiddle.net/hyygm9v/

#1


1  

var aNode = $('<a>');
aNode.attr('expr:href', 'data:post.link');
$('img.border').wrap(aNode);

#2


2  

This is working for me:

这对我很有效:

function wrapHandler() {
  return "<a expr:href='data:post.link'></a>";
}

$("img.border").wrap(wrapHandler);

I viewed the DOM after calling wrapHandler and it does add the correct wrapper. You can see it working here: https://jsfiddle.net/hyygm9vv/

我在调用wrapHandler后查看了DOM,它确实添加了正确的包装器。您可以看到它在这里工作:https://jsfiddle.net/hyygm9v/