Jquery 突出显示特定的DIV

Jquery 突出显示特定的DIV,jquery,html,highlight,Jquery,Html,Highlight,我正试图“高光”一个特定的分区 这是我的html代码 <!DOCTYPE html> <html> <head> <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/> <script src="http://aj

我正试图“高光”一个特定的分区

这是我的html代码

<!DOCTYPE html>
<html>
    <head>
        <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
        <style type="text/css">div { margin: 0px; width: 300px; height: 80px; background: #FFF; border: 1px solid black; position: relative; }</style>
        <script>
            $(document).ready(function() {
                $("a").click(function () {
                    $("div").effect("highlight", {color:"#FF0000"}, 3000);
                });
            });
        </script>
    </head>
    <body>
        <a href="#id_1"><b>id_1</b></a>
        <br/>
        <a href="#id_2"><b>id_2</b></a>

        <br/><br/><br/><br/>
        <div id="id_1"><h2>id_1 - <a class="altlink" href="#top" name="id_1" id="id_1">Top</a></h2></div>
        <div id="id_2"><h2>id_2 - <a class="altlink" href="#top" name="id_2" id="id_2">Top</a></h2></div>
    </body>
</html>

div{边距:0px;宽度:300px;高度:80px;背景:#FFF;边框:1px纯黑色;位置:相对;}
$(文档).ready(函数(){
$(“a”)。单击(函数(){
$(“div”).effect(“highlight”,{color:#FF0000},3000);
});
});






id_1- id_2-
当我用href
#id_1
单击
a
元素时,如何使其高亮显示
元素

谢谢

试试这个:

$('a[href^="#id"]').on('click', function() {
    var sHref = this.href.split('/');
    $(sHref[sHref.length - 1]).effect("highlight", {
        color: "#FF0000"
    }, 3000);
});
$('a').click(function() {
    var selector = $(this).attr('href');

    // Highlight div with whatever CSS you like
    $(selector).effect("highlight", {color:"#FF0000"}, 3000);
}
小提琴:


旁注:如果使用jQuery<1.7,则执行以下操作:

$('a[href^="#id"]').click(function() {
    var sHref = this.href.split('/');
    $(sHref[sHref.length - 1]).effect("highlight", {
        color: "#FF0000"
    }, 3000);
});
小提琴:试试这个:

$('a[href^="#id"]').on('click', function() {
    var sHref = this.href.split('/');
    $(sHref[sHref.length - 1]).effect("highlight", {
        color: "#FF0000"
    }, 3000);
});
$('a').click(function() {
    var selector = $(this).attr('href');

    // Highlight div with whatever CSS you like
    $(selector).effect("highlight", {color:"#FF0000"}, 3000);
}

这使用链接的
href
属性作为
.effect()
行的选择器。

您需要这样使用:

$(document).ready(function() {
    $("a").click(function () {
                    $(this).parent().parent().effect("highlight", {color:"#FF0000"}, 3000);
                });
            });
编辑:修复错误,放入第二个.parent()调用

$(文档).ready(函数(){$(“a#id_1”)。单击(函数(){
$(“div#id_1”).effect(“highlight”,{color:#FF0000”},3000);返回 false;});$(“a#id_2”)。单击(函数(){
$(“div#id_2”).effect(“highlight”,{color:#0000FF”},3000);返回 假;});})

HTML将有一个“A”标记 [守则]

a href=“#”id=“id1”


a href=“#”id=“id2”

为什么要突出显示父项?@Neal,因为父项是要显示的divhighlighted@mblase75你在说什么?这个问题和这个答案在任何地方都没有使用jQuery UI。@JamWaffles不是“.effect”方法的来源吗?只有当你知道你将使用jQuery 1.7+时才使用它,否则就使用
$('a[href=^“#id”]”。单击(function(){…
@mblase75我很抱歉;我没有看到。我甚至使用了
.effect()
在我自己的答案中!@JamWaffles确实如此。它使用了
效果
。我更改了答案以反映它。顺便说一句,您不应该在DOM的几个元素上使用相同的id