0

我有带有 Visual Composer 插件的 Wordpress 站点http://gambit.co/test,它允许我以 WYSIWYG 模式创建页面。它创建的所有内容都加载了 ajax 和 javascript。我有一些不错的媒体网格部分,但我无法将特定链接分配给方块。他们都是与他们的形象相连的缩影。

我试图用 jQuery 替换他们的链接

jQuery(document).ready( function($) {
$("a[href='http://gambit.co/test/wp-content/uploads/2016/07/600_wynajem.jpg']").attr('href', 'http://www.google.com/');

});

但我不能正常工作,因为脚本运行时没有 HTML 内容。我将脚本移动到页面底部,就在关闭 BODY 标记之前,但 id 不起作用。我用 .attr 和 .prop 都试过了。我应该怎么办?

4

1 回答 1

0

尝试看一下DOMNodeInserted

通过这种方式,您可以编写如下内容:

// as soon as a new anchor tag is added to the dom and
// the href value of this element is......
$(document).on('DOMNodeInserted', 'a[href="http://gambit.co/test/wp-content/uploads/2016/07/600_wynajem.jpg"]', function(e) {
  this.href = 'http://www.google.com/';
  
  this.textContent = 'http://www.google.com/';
});


$(function () {
  $('#btn').on('click', function(e) {
    $('body').append('<a href="http://gambit.co/test/wp-content/uploads/2016/07/600_wynajem.jpg">My sample</a>');
  })
});
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script>

<button id="btn">Add link</button>

于 2016-07-08T09:52:29.723 回答