2017-10-11 67 views
0

我試圖用下面的jQuery自動修復我網站文本中的窗口。這是在WordPress,這就是爲什麼我使用無衝突模式。在元素上使用jQuery,但不影響兒童

jQuery('p,h1,h2').not('a').each(function(){ 
    var string = jQuery(this).html(); 
    string = string.replace(/ ([^ ]*)$/,' $1'); 
    jQuery(this).html(string); 
}); 

的問題是,這也影響了與HREF之間的空間內段的任何鏈接,使其成爲一個  HREF,它打破了鏈接。我嘗試添加.not('a')以防止它在鏈接上運行,但它無濟於事。我也試過'p'a',認爲它只會指定那些也不起作用的p的孩子。

是否可以從該腳本中排除「href」,以便它不影響鏈接?

回答

0

嘗試:

$('p,h1,h2').each(function(){ 
    var string = $(this).html(); 
    if(!$(string).is('a')) { 
     var string_out = string.replace(/ ([^ ]*)$/,' $1'); 
     $(this).html(string_out); 
     console.log([string, string_out]); 
    } 
}); 

合作鏈接:https://jsfiddle.net/mscdeveloper/k562v82w/