English 中文(简体)
finding clicked id deep in document using jquery
原标题:

I m going to use the text from the jquery example, For example, consider the HTML:

<lots of divs to get to here>

<div id="#targetid_0">
  Click here
</div>
<div id="#targetid_1">
  Trigger the handler
</div>

I have a series of questions that are plaguing me...

Assuming that I click on Click Here or Trigger the Handler :

  1. If I m assigning a value via the attr(targetid_x,JSON.ID), how can I use alert to show me that value? It s driving me nuts!
  2. How do I find out the specific clicked #tag? (sort of related to question 1).

I d like to see if this can be accomplished with Event Delegation or at least without classes.

Halp!

最佳回答
$( div ).click(function() {
    $(this).attr( id );
});
问题回答

Try this:

$( div[id] ).click(function(){
  alert(this.id);
  return false;
});

This adds an event listener to all divs that have an id.
The return false; part stops propagation. Thus if you have nested divs that have id s only the bottom(inner) one will show the alert, and then stops the event bubbling.





相关问题
JQuery/MVC Search Issue

I have inherited a piece of work where the entry screen shows a summary of 20 calculated variables. E.g. Var A (250), Var B (79). Clicking on any of these links takes the user to a view with a ...

jQuery quicksearch plug-in tinkering with JSON

I ve implemented the quicksearch plugin by Rik Lomas and I love it for an application in a custom CMS I m building. I was wondering though, since I m going to have a bizillion items in the table if ...

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

PHP json_decode question

i m trying to use json_decode to combine a few json objects and then re-encode it. my json looks like: { "core": { "segment": [ { "id": 7, "...

Converting JSON data to Java object

I want to be able to access properties from a JSON string within my Java action method. The string is available by simply saying myJsonString = object.getJson(). Below is an example of what the string ...

热门标签