2

this is my code :

<p ><script>console.log($(this).parent())</script></p>
<p ><script>console.log($(this).parent())</script></p>
<p ><script>console.log($(this).parent())</script></p>
<p ><script>console.log($(this).parent())</script></p>

but this code cant get the

element , it show []

so how can i get the p element that dont add any classname in it ,

thanks

updated

it is ok now , use this code :

$("script").last().parent()

3 Answers 3

1

At the time of execution this points to the window object.

What you can do is give the script tag a ID.

<p><script id="scr">console.log($('#scr').parent())</script></p>

Working sample: http://jsfiddle.net/Y86fa/1/

Sign up to request clarification or add additional context in comments.

2 Comments

Because i have many <p> , so i cant get the current <p> use this method .
@zjm1126 If you can please explain what you are really trying to do, i am pretty sure there is a better way to go about it.
0

use this...

$(this).parents()

parent() is used for immediate parent.

Comments

0

it is ok now :

<p class="1"><script>console.log($("script").last().parent())</script></p>
<p class="2"><script>console.log($("script").last().parent())</script></p>
<p ><script>console.log($("script").last().parent())</script></p>
<p class="4"><script>console.log($("script").last().parent())</script></p>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.