Javascript replaceWith()不';不要在里面工作
我有一个jQuery脚本,它在鼠标悬停时替换Javascript replaceWith()不';不要在里面工作,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有一个jQuery脚本,它在鼠标悬停时替换.class ul li,并在鼠标悬停时恢复为默认值。我的问题是,当鼠标离开li时,第二个函数属性不起作用,不会返回原始的li,而替换的li将取而代之 脚本: $('.header-nav li:nth-child(2)').hover( function() { $( this ).replaceWith('<li><div class="new-messages-f"></div><a
.class ul li
,并在鼠标悬停时恢复为默认值。我的问题是,当鼠标离开li
时,第二个函数属性不起作用,不会返回原始的li
,而替换的li
将取而代之
脚本:
$('.header-nav li:nth-child(2)').hover(
function() {
$( this ).replaceWith('<li><div class="new-messages-f"></div><a href="">Account</a></li>'); // Replaced
},function() {
$( this ).replaceWith('<li><div class="new-messages-f">89</div><a href="">Ac</a></li>'); // Original
});
$(“.标题导航li:n子项(2)”。悬停(
函数(){
$(this.replaceWith(“”);//已替换
},函数(){
$(this).replace为(“89 ”);//原件
});
HTML
- 89
替换元素时,将从中删除事件绑定。我可以建议在li
元素上使用.html()
而不是replacetwith
:
$('.header-nav li:nth-child(2)').hover(
function() {
$( this ).html('<div class="new-messages-f">77</div><a href="">Account</a>'); // Replaced
},function() {
$( this ).html('<div class="new-messages-f">89</div><a href="">Ac</a>'); // Original
});
$(“.标题导航li:n子项(2)”。悬停(
函数(){
$(this.html('77');//已替换
},函数(){
$(this.html('89');//原件
});
检查此小提琴:当您替换元素时,您正在从中删除事件绑定。我可以建议在
li
元素上使用.html()
而不是replacetwith
:
$('.header-nav li:nth-child(2)').hover(
function() {
$( this ).html('<div class="new-messages-f">77</div><a href="">Account</a>'); // Replaced
},function() {
$( this ).html('<div class="new-messages-f">89</div><a href="">Ac</a>'); // Original
});
$(“.标题导航li:n子项(2)”。悬停(
函数(){
$(this.html('77');//已替换
},函数(){
$(this.html('89');//原件
});
检查此小提琴:当您替换元素时,您正在从中删除事件绑定。我可以建议在
li
元素上使用.html()
而不是replacetwith
:
$('.header-nav li:nth-child(2)').hover(
function() {
$( this ).html('<div class="new-messages-f">77</div><a href="">Account</a>'); // Replaced
},function() {
$( this ).html('<div class="new-messages-f">89</div><a href="">Ac</a>'); // Original
});
$(“.标题导航li:n子项(2)”。悬停(
函数(){
$(this.html('77');//已替换
},函数(){
$(this.html('89');//原件
});
检查此小提琴:当您替换元素时,您正在从中删除事件绑定。我可以建议在
li
元素上使用.html()
而不是replacetwith
:
$('.header-nav li:nth-child(2)').hover(
function() {
$( this ).html('<div class="new-messages-f">77</div><a href="">Account</a>'); // Replaced
},function() {
$( this ).html('<div class="new-messages-f">89</div><a href="">Ac</a>'); // Original
});
$(“.标题导航li:n子项(2)”。悬停(
函数(){
$(this.html('77');//已替换
},函数(){
$(this.html('89');//原件
});
检查一下这把小提琴:出于某种原因,我认为这种方式没有那么麻烦,看看:
$('.header-nav-li:nth-child(2)').on('mouseenter',function(){
$(this.find('.new-messages-f').html('77');
$(this.find('a').html('Account');
$(this.find('a').attr(“href”,”http://www.google.com/");
});
$('.header-nav-li:n子项(2)').on('mouseleave',function(){
$(this.find('.new-messages-f').html('89');
$(this.find('a').html('Ac');
$(this.find('a').attr(“href”,”http://www.yahoo.com/");
});代码>
89
出于某种原因,我认为这种方式没有那么麻烦,看看:
$('.header-nav-li:nth-child(2)').on('mouseenter',function(){
$(this.find('.new-messages-f').html('77');
$(this.find('a').html('Account');
$(this.find('a').attr(“href”,”http://www.google.com/");
});
$('.header-nav-li:n子项(2)').on('mouseleave',function(){
$(this.find('.new-messages-f').html('89');
$(this.find('a').html('Ac');
$(this.find('a').attr(“href”,”http://www.yahoo.com/");
});代码>
89
出于某种原因,我认为这种方式没有那么麻烦,看看:
$('.header-nav-li:nth-child(2)').on('mouseenter',function(){
$(this.find('.new-messages-f').html('77');
$(this.find('a').html('Account');
$(this.find('a').attr(“href”,”http://www.google.com/");
});
$('.header-nav-li:n子项(2)').on('mouseleave',function(){
$(this.find('.new-messages-f').html('89');
$(this.find('a').html('Ac');
$(this.find('a').attr(“href”,”http://www.yahoo.com/");
});代码>
89
出于某种原因,我认为这种方式没有那么麻烦,看看:
$('.header-nav-li:nth-child(2)').on('mouseenter',function(){
$(this.find('.new-messages-f').html('77');
$(this.find('a').html('Account');
$(this.find('a').attr(“href”,”http://www.google.com/");
});
$('.header-nav-li:n子项(2)').on('mouseleave',function(){
$(this.find('.new-messages-f').html('89');
$(this.find('a').html('Ac');
$(this.find('a').attr(“href”,”http://www.yahoo.com/");
});代码>
89
不要添加和删除元素,而是将两个版本都放在HTML中,并使用.hide()
和.show()
$('.header-nav-li:nth-child(2)')。悬停(函数(){
$(this.find(“.in”).show();
$(this.find(“.out”).hide();
},函数(){
$(this.find(“.in”).hide();
$(this.find(“.out”).show();
});代码>
.header导航输入{
显示:无;
}
-
89
89
不要添加和删除元素,而是将两个版本都放在HTML中,并使用.hide()
和.show()
$('.header-nav-li:nth-child(2)')。悬停(函数(){
$(this.find(“.in”).show();
$(this.find(“.out”).hide();
},函数(){
$(this.find(“.in”).hide();
$(this.find(“.out”).show();
});代码>
<代码>.header nav.in{
显示:无;
}
-
89
89
不要添加和删除元素,而是将两个版本都放在HTML中,并使用.hide()
和.show()
$('.header导航李:第n个子项(2)').h