简体   繁体   English

如何选择没有Id属性的元素?

[英]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.

相关问题 如何使用jQuery查找具有ID属性的下一个元素 - How to find the next element which has an ID attribute using jQuery jQuery不会选择具有动态ID的元素,该怎么做? - jQuery doesn't select the element with dynamic ID, how to do that? 将ID分配给仅具有名称属性的Input元素 - Assign id to Input element which has only name attribute 如何使用data属性拖动元素并将其放在具有相同id的框中 - How to drag element with data attribute and drop it in the box which has the same id 如何选择没有类别组合的元素? - How to select the element which has not combinations of classes? 如何分配select2插件来选择具有插件选项作为data属性的json对象的元素? - How can I assign select2 plugin to select element which has plugin options as json object at data attribute? 如何找到 select 下拉列表的值和 ID,该下拉列表已动态创建并存在于另一个元素中 - How to find the value and id of select drop-down which has been created dynamically and which is present within the another element 如何获取动态给出ID的元素的ID? - How to grab ID of element which ID has been dynamically given? HTML select元素不显示更新的selected属性 - HTML select element doesn't show updated selected attribute 如何在没有元素ID的下拉列表中选择带有Javascript的选项? - How do I select an option with Javascript in a dropdown that doesn't have an element ID?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM