使用jQuery“突出显示"光标下的内容 [英] Using jQuery to "highlight" content under cursor

查看:89
本文介绍了使用jQuery“突出显示"光标下的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一些jQuery代码,通过在其周围添加边框来突出显示光标当前悬停的元素.这是我到目前为止的代码:

I'm trying to write some jQuery code that will highlight the element the cursor is currently hovering over by adding a border around it. Here is the code I have so far:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Hover Test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>
    $(function() {
        $("*:not(html, head, body)").hover( function () {
            $(this).css("border", "2px solid purple");
        }, 
        function () {
            $(this).css("border", "none");
        }).click( function () {
            alert($(this).html());
        });
    });
</script>
</head>

<body>
<div>
    <p>This is paragraph one</p>
    <p>This is paragraph two</p>
</div>
<span id="curtag"></span>
</body>
</html>

问题是,当我将鼠标悬停在下面示例中的某个段落上时,它还会突出显示父标记(在这种情况下为div).另外,当我单击该段落时,它给了我p的html,然后给了div的html,但是,我只希望p标记中的html.有关如何解决此问题的任何建议?

The problem is when I hover over something like a paragraph in the example below it also highlights the parent tag in this case the div. Additionally, when I click on the paragraph it gives me the html of the p and then the html of the div, however, I only want the html in the p tag. Any suggestions on how to fix this?

推荐答案

添加

return false;

像这样

$(function() {
    $("*:not(html, head, body)").hover( function () {
            $(this).css("border", "2px solid purple");
    return false;
    }, 
    function () {
            $(this).css("border", "none");
    }).click( function () {
            alert($(this).html());
            return false;
    });
});

停止mouseover事件从起泡到父级.

to stop the mouseOver event from bubbeling to the parent.

这篇关于使用jQuery“突出显示"光标下的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆