2016-07-08 120 views
0

我使用Visual Composer插件創建了Wordpress站點http://gambit.co/test,該插件允許我以所見即所得模式創建頁面。所有由它創建的內容都加載了ajax和javascript。我有一些不錯的媒體網格部分,但我不能指定特定的鏈接到廣場。他們都是連接到他們的圖像的小圖片。更改動態創建鏈接的href

我試圖用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。我該怎麼辦?

+0

'內容加載Ajax和JavaScript;'所以這是否意味着其要過得去的代碼加載你寫的使用'$ .ajax';還是其他的東西? – vijayP

回答

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>