Selecting an element and anything inside it
I have a click event and an if statement within that as shown below.
if (!$(e.target).is("textarea.textobject, div.texteditor"))
{
// do stuff
}
It works but the problem is the div shown below. When I click on eithe开发者_运维知识库r the span or table inside the div it doesn't work. I have tried (div.texteditor > *) but that doesn't work.
<div class="texteditor">
<span>1</span>
<table>
<tr><td></td></tr>
</table>
</div>
So basically I want to be able to select div.texteditor and it to encompass everything inside it. Does anyone know a solution to this? I'm sure i'm missing something..
If the event handler is on the <div>
itself, just use this
instead of e.target
(which may be a child), like this:
if (!$(this).is("textarea.textobject, div.texteditor"))
{
// do stuff
}
...if instead you want to check if the target is anywhere inside one of those 2 selector types, use .closest()
and .length
, like this:
if ($(e.target).closest("textarea.textobject, div.texteditor").length == 0)
{
// do stuff
}
Since .closest()
works on the current elements and parents, it's pretty handy here.
I don't know, but have you tried to create another events assigned to the elements inside the div? Something like:
if ( $(e.target).is("span") && $(e.target).parent("div.texteditor") )
{
// do stuff
}
I don't if it works, but it's an idea!
I hope it helps you. ^^
If you want to apply to all children, this could work:
if ( $(e.target).parent("div.texteditor") )
{
// do stuff
}
精彩评论