$(document).ready(function () {
$("href").attr('href', 'title');
});
$('a[href$=.jpg]').each(function () {
var imageSrc = $(this).attr('href');
var img = $('<img />').attr('src', imageSrc).css('max-width', '300px').css('max-height', '200px').css('marginBottom', '10px').css('marginTop', '10px').attr('rel', 'lightbox');
$(this).replaceWith(img);
});
});这是我目前拥有的jQuery代码,在将它们嵌入页面之前,我希望将所有链接的href更改为与它们的标题相同的href。然而,随着代码中将href更改为title位,它将停止工作。我是Javascript的新手,所以我肯定做错了什么,只是还不确定是什么!任何帮助都非常感谢!
谢谢你们
编辑
这是我想要更改的html:
<p class="entry-content">Some interesting content<a href="http://example.com/index.php/attachment/11" title="example.com/file/testing-20101016T114047-2k5g3ud.jpeg" rel="external" class="attachment" id="attachment-11">http://example.com/index.php/attachment/11</a></p>发布于 2010-10-16 22:22:33
您更改它是错误的,您正在尝试选择href元素而不是a。
这个修复应该可以做到:
$("a[title]").each(function() {
$(this).attr('href',$(this).attr('title'));
});它将使用title选择所有a元素,并使用此值设置href。
发布于 2010-10-16 22:20:46
尝试:
$("a").each(function () {
var $this = $(this);
$this.attr('href', $this.attr('title'));
});发布于 2010-10-16 22:23:53
这一行:
$("href").attr('href','title');正在查找所有标题元素,并将它们的标题属性替换为字符串‘href’。因为没有href元素这样的东西,所以尝试这样做:
// for every anchor element on the page, replace it's href attribute with it's title attribute
$('a').each(function() {
$(this).attr('href', $(this).attr('title');
});https://stackoverflow.com/questions/3949232
复制相似问题