我正在尝试选择输入元素,但它似乎不起作用。
.HTML:
<section id="c1">
<div class="input-group col-lg-2">
<span class="input-group-addon">
<i class="glyphicon glyphicon-map-marker"></i>
</span>
<input type="text" readonly class="form-control"/>
<span class="input-group-btn">
<button class="btn btn-default" type="button">GPS</button>
</span>
</div>
</section>
j查询:
$("#c1").find("input");
我正在尝试选择这个:
<input type="text" readonly class="form-control"/>
它
对我来说很好用:
当我使用此 HTML 时...
<section id="c1">
<div class="input-group col-lg-2">
<span class="input-group-addon">
<i class="glyphicon glyphicon-map-marker"></i>
</span>
<input type="text" readonly class="form-control"/>
<span class="input-group-btn">
<button class="btn btn-default" type="button">GPS</button>
</span>
</div>
</section>
。而这个JS...
var $InputElement = $("#c1").find("input");
console.log($InputElement);
。控制台将精确返回您要查找的输入元素。
好吧,实际上,它返回一个包含您要查找的元素的jQuery对象。
如果你想要 DOM 元素本身而不是 jQuery 对象,只需这样做:
var InputElement = $("#c1").find("input")[0];
console.log(InputElement);
您可以在此小提琴中自己尝试。