Javascript 无法使用jQuery选择器获取属性
我想知道为什么我不能使用jQuery选择器来获取如下属性:Javascript 无法使用jQuery选择器获取属性,javascript,jquery,Javascript,Jquery,我想知道为什么我不能使用jQuery选择器来获取如下属性: <input type="text" class="inputText" value="Enter text..."> console.log($('.inputText').getAttrbute('value')); jQuery方法是: 您正在寻找jQuery的方法 您可以在jQuery对象上使用val(),也可以在JavaScript对象上使用getAttribute console.log($('.inputTe
<input type="text" class="inputText" value="Enter text...">
console.log($('.inputText').getAttrbute('value'));
jQuery方法是:
您正在寻找jQuery的方法 您可以在jQuery对象上使用val(),也可以在JavaScript对象上使用getAttribute
console.log($('.inputText').val());
您还可以在对象之后使用[0]从jquery对象获取JavaScript对象
console.log($('.inputText')[0].getAttrbut('value'));
要获取任何属性,可以使用attr(),但如果要查找输入值,请使用val()问题是$('.inputText')
为您提供jquery对象,而不是直接的dom元素。此外,dom元素没有属性getAttrbut()
,而是它的getAttribute()
你可以得到它:
console.log($('.inputText').val());//recommended
console.log($('.inputText').attr('value'));
console.log($('.inputText').prop('value'));
console.log($('.inputText').get(0).getAttribute('value')); //use get() to access direct dom element.
jquery没有任何名为“getAttrbute”的方法。但若您需要访问该属性,那个么可以像这样使用“attr”
$(".inputT").attr("AttributeName");
或者,如果要删除属性,请使用
$(".inputT").removeAttr("AttributeName");
即使jQuery有一个同名的函数,它仍然缺少一个“i”和一个“e”
getattr,但实际上不是。至少不太合适。谢谢。我认为我混淆了JavaScript对象和jQuery对象。
$(".inputT").attr("AttributeName");
$(".inputT").removeAttr("AttributeName");