javascript - 向导致 attr 的 HTML 元素添加属性不是函数

标签 javascript jquery html this

我有一个 ID 为 containerButtons 的 HTML 元素,其中有许多 labelsdiv:

<div class="button-grp" id="containerButtons">
    <label>Selector 1</label>
    <label>Selector 2</label>
    <label>Selector 3</label>
    <div class="time-container">
        <input type="number" min="0" max="24" step="1" value="00" id="time-hour" disabled="disabled">
        <span>:</span>
        <input type="number" min="0" max="60" step="1" value="00" id="time-minutes" disabled="disabled">
    </div>
</div>

上面是通过文件 buttons.js 中的 JS 函数创建的。

按钮.js:

function ButtonsPanel() {

    var that = this;
    this.buttonsEl = document.createElement('div');

    var baseBtn = d3.select(this.buttonsEl);

    var containerBtns = baseFilter.append('div')
        .classed({'button-grp': true})
        .attr({id:'containerButtons'});


    var tmp = containerBtns.append('div')
        .classed({'time-container': true});

    tmp.append('input')
        .attr({type: 'number', min: '0', max: '24', step:'1', value:'00', id:'time-hour', 'disabled': 'disabled'});

    tmp.append('span')
        .text(':');

    tmp.append('input')
        .attr({type: 'number', min: '0', max: '60', step:'1', value:'00', id:'time-minutes', 'disabled': 'disabled'});

    this.timeInputs = tmp;

    var timeHours = this.timeInputs[0][0].children[0];
    // returns <input type="number" min="0" max="24" step="1" value="00" id="time-hour" disabled="disabled">
    var timeMins = this.timeInputs[0][0].children[2];
    // returns <input type="number" min="0" max="60" step="1" value="00" id="time-minutes" disabled="disabled">

}

在 buttons.js 中,我有一个在加载时调用的方法 toggleVisability:

ButtonsPanel.prototype.toggleVisability = function() {

    var that = this;

    this.visabilityApply('#containerButtons', function(num) {
        if (!num) {
            alert(that.timeInputs[0][0].children[0]);
            that.timeInputs[0][0].children[0].attr({'disabled': true});
        }
        else {
            alert(that.timeInputs[0][0].children[0]);
            that.timeInputs[0][0].children[0].attr({'disabled': null});
        }
    });
};

有了上面的,我得到错误:

TypeError: that.timeInputs[0][0].children[0].attr is not a function

但是,如果我申请:

that.timeInputs.attr({'disabled': true});

以上工作正常,但是,我想切换输入字段上的禁用属性。我正在使用 jQuery。

最佳答案

attr() 是一个 jQuery 函数,因此需要在 jQuery context 上调用。

在你的情况下:

that.timeInputs[0][0].children[0]

返回一个不公开 attr() 函数的 native DOM 元素,因此返回错误(而 that.timeInputs 仍然引用 jQuery 上下文)。

试试这个:

$(that.timeInputs[0][0].children[0]).attr({'disabled': true});

关于javascript - 向导致 attr 的 HTML 元素添加属性不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36401179/

相关文章:

javascript - 输入变化时更新 Vue 减法方法

javascript - 如何从 backbone.js 中的模型获取数组元素

Jquery 内联日期选择器选择多个日期

javascript - 使用 ajax 加载的内容不会触发 jQuery 脚本

html - 在bootstrap中匹配一个dd的 "last-line"

javascript - 创建模板并在 JS 中使用变量进行替换

javascript - Firebase 更新对比集

javascript - jquery/javascript - 简单的 split() 问题

javascript - 理解 New 与 JavaScript 作用域函数并传递 $

css - 使用显示 :table for layout/presentational purposes?