0

我有一段里面有粗体字

<p style="class1"><b>Some text</b> some other text</p>

mouseenter和元素mouseout事件的jQuery 监听器class1

$('class1').mouseenter(function(){
    $(this).addClass("highlight");
});

$('class1').mouseout(function(){
    $(this).removeClass("highlight");
});

其中highlight类是模拟经典 HTML 选择的样式。

.highlight{
   background-color: blue;
   color: white !important
}

(我没有使用 style1:hover 因为我想以编程方式模拟也用箭头选择,但这现在并不重要)

所以,我有一些这样的段落:

一些文字一些其他文字

一些文字一些其他文字

一些文字一些其他文字

现在,我的问题是,当鼠标光标从粗体文本传递到普通文本(或反之亦然)时,mouseout事件触发,失去我的假选择,但停留在同一段落中。我怎样才能避免这种行为?

4

2 回答 2

2

您必须使用 mouseleave 事件而不是 mouseout ..

$('.class1').mouseenter(function(){
    $(this).addClass("highlight");
});

$('.class1').mouseleave(function(){
    $(this).removeClass("highlight");
});

这将解决您的问题.. :)

于 2013-07-05T13:55:13.563 回答
0

我不明白这个问题..但是让我建议你......永远不要使用标签!如果你想要一个粗体的文本,在它上面加上一个跨度并使用 css font-weight:bold; ..

于 2013-07-05T13:55:46.280 回答