【问题标题】:How to find element by id in Angualar's jqLite element object如何在 Angular 的 jqLit​​e 元素对象中通过 id 查找元素
【发布时间】:2016-01-24 11:28:20
【问题描述】:

我正在使用这个:

$vehicleTypeDropdown = element.find(".vehicleTypeDropdown");

然后在稍后的时间点我想找到#pselect ID 元素。 我的$vehicleTypeDropdown 看起来像这样,它是 jqLit​​e 对象:-

<select class="vehicleTypeDropdown" id="vehicleType">
<option id="#pselect" value="">Please Select</option>
<option ng-repeat="types in selectedType" value="Vt3" class="ng-scope ng-binding">vt3</option>
<option ng-repeat="types in selectedType" value="Vt4" class="ng-scope ng-binding">vt4</option>
</select>

有两个问题-

  1. jqLit​​e 文档中写道,.find() 方法只查找标签,而不查找类或 ID。那么我如何获得

    $vehicleTypeDropdown = element.find(".vehicleTypeDropdown");

    作为带有内容的jqLite 对象?

  2. 我怎样才能找到#pselect 的选项?我想手动删除它,请记住它可以按选项中的任何顺序排列。

【问题讨论】:

  • 你不能把它们分别分配给一个变量吗? var $ps = $('#pselect'); 和 var $vtd = $('.vehicleTypeDropdown');
  • 你能解释一下为什么你需要这个吗?通常在使用 angularjs 时不需要手动 DOM 操作
  • @zer00ne,jqLit​​e,与 jQuery 不同
  • @Grundy 那么它在 jq 上非常精简,应该叫 jqNot
  • @zer00ne,是的,如果 jQuery 不包含,您的注释中的代码将不起作用

标签: javascript angularjs jqlite


【解决方案1】:

使用不带# 的id,例如id="pselect"

<select class="vehicleTypeDropdown" id="vehicleType">
    <option id="pselect" value="">Please Select</option>
    <option ng-repeat="types in selectedType" value="Vt3" class="ng-scope ng-binding">vt3</option>
    <option ng-repeat="types in selectedType" value="Vt4" class="ng-scope ng-binding">vt4</option>
</select>

像这样捕捉元素

var elem = angular.element(document.querySelector('#pselect'))
elem.remove()

http://jsfiddle.net/vorant/4sbux96k/1/

【讨论】:

  • @Grundy This Answer 建议避免在 id 属性中使用 #。它有效,但不推荐。
  • @georgeawg,是的,我并不是说这是最好的,我只是提到getElementById这项工作:-)
【解决方案2】:

如何在 Angular 的 jqLit​​e 元素对象中通过 id 查找元素

Id 在页面上应该是唯一的,因此您可以使用document.getElementById 或document.querySelector 找到它,然后将其包装在 jqLit​​e 元素中。

var pselect = angular.element(document.getElementById('pselect'));

这适用于:

<option id="pselect" value="">

但如果你有 html:

<option id="#pselect" value="">

你应该使用:

var pselect = angular.element(document.getElementById('#pselect'));

我只想找到它并动态删除它,以便用户无法在选择中看到该选项。

在这种情况下,您可以简单地使用ng-if 指令:

<select class="vehicleTypeDropdown" id="vehicleType">
    <option id="pselect" ng-if="condition" value="">Please Select</option>
    <option ng-repeat="types in selectedType" value="Vt3" class="ng-scope ng-binding">vt3</option>
    <option ng-repeat="types in selectedType" value="Vt4" class="ng-scope ng-binding">vt4</option>
</select>

如果条件为真,元素添加到 DOM,如果不是 - 不是


如果此代码有效:
$vehicleTypeDropdown = element.find(".vehicleTypeDropdown");

你得到了正确的对象,然后在你的页面上还包含完整的 jQuery,因为正如你在 jqLit​​e 源代码中看到的那样,find function 很简单

find: function(element, selector) {
    if (element.getElementsByTagName) {
        return element.getElementsByTagName(selector);
    } else {
        return [];
    }
},

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多