[英]How to select element which doesn't has Id attribute?
I have two textarea and one of them has id
attribute and other one doesn't has. 我有两个textarea,其中一个有
id
属性,另一个没有。 Something like this: 像这样的东西:
<textarea> </textarea>
<textarea id = 'idname'> </textarea>
Now I need to select first textarea, How can I do that? 现在我需要选择第一个textarea,我该怎么做?
You could combine the :not()
pseudo-class and the [id]
attribute selector in order to negate elements with an id
attribute: 您可以组合
:not()
伪类和[id]
属性选择器 ,以便使用id
属性否定元素:
textarea:not([id]) {}
document.querySelectorAll('textarea:not([id])');
$('textarea:not([id])');
Basic Example: 基本示例:
textarea:not([id]) { width: 100%; }
<textarea></textarea> <textarea id='idname'></textarea>
As you also don't mind a jQuery answer: 你也不介意jQuery的答案:
$('textarea:not([id])');
or 要么
$('textarea').not('[id]');
Update: 更新:
For a specific name: 对于特定名称:
$('textarea[name="somename"]');
Try to use the following in your case: 尝试在您的情况下使用以下内容:
$('textarea:not(#idname)')[0]
Selector depends on exact requirements and mark-up 选择器取决于确切的要求和标记
JQuery solution : JQuery解决方案:
Using :not() Selector : 使用:not()选择器 :
$('textarea:not(#idname)')
Hope this helps. 希望这可以帮助。
$('textarea:not(#idname)').text('selected');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea> </textarea> <textarea id='idname'> </textarea>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.