admin管理员组

文章数量:1414908

I have a .button made in css and added to the html 4 times like this

<a class="button icon Call"   id="nupp2" href="#"><span>CALL</span></a>

and a .js with the following inside

$(document).ready(function(){
  $("nupp2").click(function(){
      var name=prompt("Please enter your name","blabla");
  });
});

The buttons appear if I open the html with firefox and they change if I hover over them But if I press the button, it doesn't do anything. I didn't forget to point to the files in the html file.

Am I even doing this right? Or is jquery more plex than I think?

I have a .button made in css and added to the html 4 times like this

<a class="button icon Call"   id="nupp2" href="#"><span>CALL</span></a>

and a .js with the following inside

$(document).ready(function(){
  $("nupp2").click(function(){
      var name=prompt("Please enter your name","blabla");
  });
});

The buttons appear if I open the html with firefox and they change if I hover over them But if I press the button, it doesn't do anything. I didn't forget to point to the files in the html file.

Am I even doing this right? Or is jquery more plex than I think?

Share Improve this question edited Mar 25, 2011 at 16:02 Prisoner 27.7k11 gold badges76 silver badges103 bronze badges asked Mar 25, 2011 at 16:00 nilsnils 3353 gold badges5 silver badges16 bronze badges 1
  • 2 You need to use a unique id value for each button... otherwise use a class. – prodigitalson Commented Mar 25, 2011 at 16:03
Add a ment  | 

7 Answers 7

Reset to default 4

Selectors in jQuery work a lot like the ones in CSS. $("nupp2") bees $("#nupp2"), see?

This is wrong:

$("nupp2").click(function(){

The correct is:

$("#nupp2").click(function(){

The string inside the parens is a jQuery selector. Since you want to select an element by id, the proper selector is a hash sign followed by the id.

you need to add a hash sign (#) before the ID of the element - $('#npp')

You missed the hash on your selector:

$("#nupp2").click(function(){        // <----- #nupp2
    var name=prompt("Please enter your name","blabla");
});

To call an ID you need to add a # in front of the selector (Like CSS)

So your jQuery selector should be $("#nupp2")

Just try this

$(document).ready(function(){
  $("#nupp2").click(function(){
      var name=prompt("Please enter your name","blabla");
  });

Try this:

 $(document).ready(function(){
  $(a#nupp).click(function(){
      var name=prompt("Please enter your name","blabla");
  });
});

本文标签: javascriptHow to make jquery click() work with css buttonStack Overflow