简体   繁体   中英

jquery simple onclick event

<a aria-expanded="false" data-toggle="tab" href="#pictures">
   <i class="pink ace-icon fa fa-gift bigger-120"></i>
    Pictures
</a>

I want to wire on click #picture event to alert some message so I add

 $(document.ready(function() {
        $("#pictures").click(function(){
            alert('clicked!');               
        });
 })

but this doesnt work, I've got no error messages inside firebug console. Jquery is loaded before correctly.

In your html there is no element with ID pictures , change it, like so

 $("#pictures").click(function(){ alert('clicked!'); }); // or use selector like this, if you can't change html $('a[href="#pictures"').click(function(){ alert('clicked!'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a aria-expanded="false" data-toggle="tab" href="#pictures" id="pictures"> <i class="pink ace-icon fa fa-gift bigger-120"></i> Pictures </a> 

Update:

Also you have error in this line

$(document.ready(function() {

change it

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

Did You try tout set the click handler on document?

jQuery(document).on('click',  '#pictures', function(e) {
     alert('clicked');
});

You also have to add :

id="pictures"

In your . Or an y other selector.

If you want to still use the #picture in href check on bellow code

 jQuery(document).ready(function($) {
     $("a[href='#pictures']").click(function(){
         alert('clicked!');   
         return false;            
     });
 })

Try this Demo , even changed some markup for the same.

$(document).ready(function() {
        $("#pictures").click(function(){
            alert('clicked!');               
        });
 })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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