嗨,我在使用 JQuery 的 .append .prepend 方法时遇到问题我非常了解这些方法,但是当我尝试这样的代码时发生了一个奇怪的故障:
$("#nashgraphics-menu ul li a.toplevel-a")
.prepend('<span class="main-navigation-button-left"></span><span class="main-navigation-button-body"><h1>');
$("#nashgraphics-menu ul li a.toplevel-a")
.append('</h1></span><span class="main-navigation-dropdown-button-right"></span>');
它应该可以正常工作,但不幸的是它没有,我想知道为什么
注意:我说的是“链接”文本,这些标签应该包含该文本,但它没有
代码应该输出这个:
<span class="main-navigation-button-left"></span>
<span class="main-navigation-button-body"><h1>Link</h1></span>
<span class="main-navigation-dropdown-button-right"></span>
但它是这样的:
<span class="main-navigation-button-left"></span>
<span class="main-navigation-button-body"><h1></h1></span>Link<span class="main-navigation-dropdown-button-right"></span>
该文本“链接”应该在标签内
顺便说一下,这是我在 Wordpress 中使用的整个代码
;(function($){
$.fn.nashgraphics_menu = function(){
$("#nashgraphics-menu ul li").each(function(index){
$('#nashgraphics-menu ul li').addClass('toplevel-nochild');
$('#nashgraphics-menu ul li ul li').removeClass('toplevel-nochild').addClass('nochild');
$('#nashgraphics-menu ul li:has(ul)').removeClass('toplevel-nochild nochild').addClass('toplevel');
$('#nashgraphics-menu ul li ul li:has(ul)').removeClass('toplevel nochild').addClass('haschild');
$('#nashgraphics-menu ul li a').addClass('toplevel-nochild-a');
$('#nashgraphics-menu ul li ul li a').removeClass('toplevel-nochild-a').addClass('nochild-a');
$('#nashgraphics-menu ul li:has(ul) > a').removeClass('toplevel-nochild-a nochild-a').addClass('toplevel-a');
$('#nashgraphics-menu ul li ul li:has(ul) > a').removeClass('toplevel-a nochild-a').addClass('haschild-a');
});
$("#nashgraphics-menu ul li a.toplevel-a").prepend('<span class="main-navigation-button-left"></span><span class="main-navigation-button-body"><h1>');
$("#nashgraphics-menu ul li a.toplevel-a").append('</h1></span><span class="main-navigation-dropdown-button-right"></span>');
$('#nashgraphics-menu ul li a.haschild-a').append(' »');
};
})(jQuery);