JS:
this.par = $(this).find("p");
HTML:
<p></p>
The problem is that I dont want to find p
tag, but rather a div
with a specific ID like this one below.
<div id="abc"></div>
JS:
this.par = $(this).find("p");
HTML:
<p></p>
The problem is that I dont want to find p
tag, but rather a div
with a specific ID like this one below.
<div id="abc"></div>
Share
Improve this question
edited Sep 8, 2014 at 7:31
ngrashia
9,9045 gold badges45 silver badges58 bronze badges
asked Nov 17, 2011 at 12:53
user478636user478636
3,41416 gold badges51 silver badges79 bronze badges
2
- 2 Surely it would have been quicker and easier for you to read the jQuery API, than to type that out and then wait for an answer? – Rory McCrossan Commented Nov 17, 2011 at 12:55
- I was using # for id but it seemed there is other error – user478636 Commented Nov 17, 2011 at 14:00
5 Answers
Reset to default 4Use the ID selector:
var myDivObj = $("#abc");
Take a look at the list of jQuery selectors.
Additional Information:
It's difficult to tell by your code what you're trying to do, but based on what you've posted, there is no reason to use $(this)
. The ID selector alone should meet your needs.
Well, you can just use the id selector:
$(this).find('#abc');
Since ids should be unique on the page, you may as well just use it as the constructor:
$('#abc');
If this isn't exactly the same, you're doing something wrong.
this.par = $(this).find("#abc");
You don't want to do that. Don't add properties to the html elements. This is better:
var par = $(this).find('#idOfElement')
Storing the result in this.par
is a very bad idea, since this
refers to a DomElement.
What you might be looking for is jQuery .data()
:
$(this).data('par', $(this).find('#idOfElement'))
Which allows you to associate #idOfElement
with this
.
use id selector
this.par = $(this).find("#abc");
but id is uniqe you can remove $(this).find and use this code
this.par = $("#abc");
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744251631a4565195.html
评论列表(0条)