开发者

jquery clickable div

开发者 https://www.devze.com 2022-12-26 07:21 出处:网络
click here I want to make that div clickable and it should trigger following function: <script type=\"text/javascript\">
click here

I want to make that div clickable and it should trigger following function:

<script type="text/javascript">
$(document).ready(function(){
    $("#generate").click(function(){
        $("#quote p").load("script.php");
    });
});
</script>开发者_运维百科

how can I make that work?


Give the div the ID generate or any other (of course reasonable) ID to uniquely select it:

<div id="someid" class="clickme"> click here </div>

<script type="text/javascript">
$(document).ready(function(){
    $("#someid").click(function(){
        $("#quote p").load("script.php");
    });
});
</script>

Note that an ID has to be unique throughout the document.

You can also just use the class to select the element, but this will select any element with this class and bind the handler to all of these:

$(document).ready(function(){
    $(".clickme").click(function(){
        $("#quote p").load("script.php");
    });
});

Read the documentation about selectors, especially ID and class selectors.


Either give the <div> an id of generate:

<div id="generate">

or replace #generate by .clickme in the JS:

$('.clickme').click(function() {
    // ...
});

This is pretty trivial. To learn more about using jQuery, I warmly recommend to go through their tutorials. If you prefer a book, then I recommend jQuery in Action.

0

精彩评论

暂无评论...
验证码 换一张
取 消