简体   繁体   English

如何在jQuery中触发点击事件?

[英]How to fire click event in jQuery?

I am a beginner in jQuery programming. 我是jQuery编程的初学者。 I am trying to fire a click event on list click, but it is not working properly. 我正在尝试在列表点击上触发点击事件,但无法正常工作。 The following is my code: 以下是我的代码:

<html>
<head>
<script type="text/javascript" >
<script type="text/javascript" src="jquery.js">

<link rel="stylesheet"  href="style.css" />
$("ul#select li").click(function(e){
    //targ = $(e.target);
    if($(this).hasClass("selected"))
    {
        $(this).removeClass("selected");
        document.location= "cambridge:clearanswer";
    }
    else
    {
        currentanswer = $(this).html();
        document.location = "cambridge:checkanswer:"+currentanswer;
        $("ul.select li").removeClass("selected");
        $(this).addClass("selected");
    }
});

function getUsed()
{
    var total = "";
    $(".boxWord.boxWordUsed").each(function (i){ total = total + this.getAttribute('index_no') + ":"; });
    return total;
}

</script>
</script>
<link href="style.css" media="all" rel="stylesheet" type="text/css" /> 
</head>
<body>
<div class="question" >
<ul data-role="listview" data-inset="true" data-theme="c" data-dividertheme="b" class="select" id="select"> 
            <li data-role="list-divider" class="selected">Overview</li> 
<li data-role="list-divider" class="selected">Overview</li> 
</ul>
</div> 
</body>
</html>

You need to wrap your script up in document ready: 您需要将脚本包装在准备好的文档中:

$(document).ready(function() {
   ...your click event code
});

Otherwise it may try to attach the event before the element is loaded. 否则,它可能会尝试在加载元素之前附加事件。

First of all you are not writing codes in <script> tag and the code messed up between multiple tags. 首先,您不是在<script>标签中编写代码,而是在多个标签之间弄乱了代码。 Then you have use $(document).ready() . 然后,您可以使用$(document).ready() You also have other errors in code, I have corrected all of them for you, I understand you are a newbie :). 您在代码中还存在其他错误,我已为您更正了所有错误,我了解您是新手:)。

See a working demo here http://jsfiddle.net/DtHJY/1/ 在此处查看有效的演示 http://jsfiddle.net/DtHJY/1/

Here is the corrected code: 这是更正的代码:

 <html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<link rel="stylesheet"  href="style.css" />

<script type="text/javascript">
    $(document).ready(function(){
$("ul#select li").click(function(e){
    alert('test');
                        //targ = $(e.target);
                        if($(this).hasClass("selected"))
                        {
                        $(this).removeClass("selected");
                        document.location= "cambridge:clearanswer";
                        }
                        else
                        {
                        currentanswer = $(this).html();
                        document.location = "cambridge:checkanswer:"+currentanswer;
                        $("ul.select li").removeClass("selected");
                        $(this).addClass("selected");
                        }
                        });




function getUsed()
{
    var total = "";
    $(".boxWord.boxWordUsed").each(function (i){ total = total + this.getAttribute('index_no') + ":"; });
    return total;
}

    });
</script>
<link href="style.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="question" >
<ul data-role="listview" data-inset="true" data-theme="c" data-dividertheme="b" class="select" id="select">
            <li data-role="list-divider" class="selected">Overview</li>
<li data-role="list-divider" class="selected">Overview</li>
</ul>
</div>
</body>
</html>

try 尝试

document.location.href = 'link'

instead 代替

and also do not forgot to wrap your code into 也不要忘记将代码包装到

$(function(){
     //your code
});

As long as you have wrapped the event binder in $(document).ready(function() { ... }); 只要将事件绑定程序包装在$(document).ready(function() { ... }); then it will work perfectly. 那么它将完美地工作。

Have a look at this demo: http://jsfiddle.net/E5BZ9/ 看一下这个演示: http : //jsfiddle.net/E5BZ9/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM