简体   繁体   English

jQuery在多个级别下查找子元素

[英]jQuery find an sub element multiple levels down

I have: 我有:

<div id="d1">
<label>title</label>
 <div class="col">
  <select id="location.state.id">...</select>
 </div>
</div>

Basically, i want to disable/enable the <select> . 基本上,我想禁用/启用<select> And I have something like: 我有类似的东西:

$("#d1").find("#location.state.id").prop("disabled", true);

It doesn't work. 没用 What am I missing? 我想念什么?

The problem is that your id attribute has a value with . 问题是您的id属性的值为. inside it. 在里面。

Since . 由于. a CSS selector for class names, jQuery will look for an item that has an ID location and the CSS classes state and id , instead of an element with the ID location.state.id . 作为类名称的CSS选择器,jQuery将查找具有ID location以及CSS类stateid ,而不是具有ID location.state.id的元素。

You must use the \\\\ before the . 您必须在之前使用\\\\ . to use them in a query selector (in CSS, only \\ would be good): 在查询选择器中使用它们(在CSS中,仅\\会很好):

$('#d1').find('location\\.state\\.id').prop('disabled', true);

Quoting jQuery API 引用jQuery API

To use any of the meta-characters (such as !"#$%&'()*+,./:;<=>?@[\\]^``{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\\\ . For example, an element with id="foo.bar" , can use the selector $("#foo\\\\.bar") . 要将任何元字符(例如!"#$%&'()*+,./:;<=>?@[\\]^``{|}~ )用作名称的文字部分,必须使用两个反斜杠将其转义: \\\\例如,具有id="foo.bar"的元素可以使用选择器$("#foo\\\\.bar")

JQuery API jQuery API

If you want to escape special characters use \\. 如果要转义特殊字符,请使用\\。 In your case: 在您的情况下:

$("#d1").find("#location\\.state\\.id").prop("disabled", true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM