开发者

How to add a text in a certain div when click in a link

This is my first incursion in jQuery so I am really newbie with this. I read the documentation (actually, I am still on it) to get to know it开发者_高级运维 better!

The thing is that I have a website with several links into a li element and I want to achieve that every time I click in one of these links, a certain piece of text will appear in a div id="container" in the webpage.

How could I do this?


Quick, simple answer:

$('#yourlist li a').click(function(){
    //grab link's href attribute
    var href = $(this).attr('href');

    //update content
    $('#container').html("Loading page " + href + "...");

    //load page via AJAX using link's href
    $('#container').load(href);
});

I added more code to show how you would access the given link using $(this) and further load its href via AJAX.


$('li a').click(function(){
   $('#container').html('Text you want to add');
});


try

<script src="your/downloaded/jquery/path.js></script>       
<script>
      $(document).ready(function() {
        $('a#idOfLink').click(function() {
            $("div#container").html("<span class='red'>Your certain text</span>");
         });
     })
</script>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜