向 HTML 元素添加属性导致 attr 不是函数
Adding attribute to HTML element causing attr is not a function
我有一个 ID 为 HTML 的元素:containerButtons
,其中有多个 labels
和 div
:
<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>
以上是通过文件中的JS函数创建的,buttons.js.
buttons.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.[=15= 上调用]
你的情况:
that.timeInputs[0][0].children[0]
Returns 原生 DOM 元素不公开 attr()
函数,因此出现错误(而 that.timeInputs
仍在引用 jQuery上下文)。
试试这个:
$(that.timeInputs[0][0].children[0]).attr({'disabled': true});
如果您仍然希望使用 Javascript ,您可以使用 setAttribute()
之类的
that.timeInputs[0][0].children[0].setAttribute("disabled", "true");
去掉括号{}
把变量包裹在jquery
$(that.timeInputs[0][0].children[0]).attr('disabled': true)
我有一个 ID 为 HTML 的元素:containerButtons
,其中有多个 labels
和 div
:
<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>
以上是通过文件中的JS函数创建的,buttons.js.
buttons.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.[=15= 上调用]
你的情况:
that.timeInputs[0][0].children[0]
Returns 原生 DOM 元素不公开 attr()
函数,因此出现错误(而 that.timeInputs
仍在引用 jQuery上下文)。
试试这个:
$(that.timeInputs[0][0].children[0]).attr({'disabled': true});
如果您仍然希望使用 Javascript ,您可以使用 setAttribute()
之类的
that.timeInputs[0][0].children[0].setAttribute("disabled", "true");
去掉括号{}
把变量包裹在jquery
$(that.timeInputs[0][0].children[0]).attr('disabled': true)