所以jQuery 1.6具有新功能prop().
$(selector).click(function(){
//instead of:
this.getAttribute('style');
//do i use:
$(this).prop('style');
//or:
$(this).attr('style');
})
Run Code Online (Sandbox Code Playgroud)
或者在这种情况下,他们做同样的事情?
如果我也有转用prop(),所有的旧attr()电话,如果我切换到1.6将打破?
UPDATE
selector = '#id'
$(selector).click(function() {
//instead of:
var getAtt = this.getAttribute('style');
//do i use:
var thisProp = $(this).prop('style');
//or:
var thisAttr = $(this).attr('style');
console.log(getAtt, thisProp, thisAttr);
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
<div id='id' style="color: red;background: orange;">test</div>Run Code Online (Sandbox Code Playgroud)
(另见这个小提琴:http://jsfiddle.net/maniator/JpUF2/)
控制台会记录getAttribute作为一个字符串,并attr作为一个字符串,但prop作为一个CSSStyleDeclaration,为什么?这对我未来的编码有何影响?
我在一些地方看到.attr()用于jQuery.在一些地方.prop()被使用.但我搜索了SO和谷歌我很困惑.请告诉我这两者之间的确切区别以及何时使用它们.
我已经看到以下链接 jQuery attr与prop,有一个道具列表? jQuery attr vs prop?
但我没有得到答案.请帮帮我.谢谢.
在给出downvote之前请说明原因,然后我将在下一篇文章中更正.
我可以,一个jQuery1.9 +软件开发人员,attr()在我的日常工作中"弃用"该方法的使用吗?
如许多问题所示,
关于"使用attr或使用prop?"有很多困惑.,并且,根据我的(开发人员)观点,对于attr()方法的所有用途,我们可以使用prop:
prop(name,newvalue)方法更改所有值.removeProp(name)方法都可以删除.attr(name)方法也会受到影响.关于prop的"强类型":它比"html string value"(例如"checked"vs true)更好.attr浏览器中的方法 返回undefined,如果不是)...嗯,我们需要在某个软件中使用它吗? 在表单中,".val()方法是建议的jQuery方式来获取或设置表单的值"所以,在这个时候(2013年),我没有看到attr在开发新的jQuery代码时使用方法的一个很好的理由 ......但是,换句话说,这就是问题:我有充分的理由attr在我的方法中使用 方法日常任务?
HTML:
<form>
<button type="button" class="form-clear">Clear</button>
<button type="button" class="form-set">Set</button>
<input type="text" value="Preset value" />
<input type="checkbox" checked/>
<input type="checkbox" />
</form>
Run Code Online (Sandbox Code Playgroud)
jQuery的:
$(".form-clear").click(function(){
$(':input').not(':button, :submit, :reset, :hidden').val('');
$(':checkbox, :radio').attr('checked', false);
});
$(".form-set").click(function(){
$(':input').not(':button, :submit, :reset, :hidden').val('New preset value');
$(':checkbox, :radio').attr('checked', true);
});
Run Code Online (Sandbox Code Playgroud)
我猜这两个函数之间有冲突吗?
这不是一个重复的问题attr,我只需要决定使用还是使用更好/更快/正确prop。最简单可靠的方法是检查列表。“使用更好的元素名称列表prop(name)和/或使用更好的列表attr(name)”。
PS:我使用的是 jQuery 1.9+,并且假设这attr()不是一个已弃用的方法。
决策示例(LIST有答案):
$(x).prop('id')是通过还是通过更好地获取 ID 值$(x).attr('id')?$(x).prop('title','BLABLA')用或 用 更改标题更好吗$(x).attr('title','BLABLA')?$(x).attr('selectedIndex')?$(x).attr('name')和$(x).prop('name')会返回不同的东西?也许完整的列表不是必需的,只需列出对 jQuery 产生一些影响或风险的事物即可。
关闭后编辑:请单击下面的重新打开链接以接受此编辑的问题文本。
我想在获取需要选择的选项值后,在jquery中选择自动选择选项.但是它无法在Chrome中运行.它在firefox和IE 9中工作.为什么会这样?函数填充是填充值的函数.(函数res,仅重置由函数填充填充的值,对此问题不重要)
function fill(thisValue) {
$('#inputString').val(thisValue);
$.post("get.php?op=category", {queryString: ""+thisValue+""}, function(data){
if(data.length >0) {
$("#mymenu option[value='"+data+"']").attr('selected', 'selected');
$('#mymenu').attr("disabled","disabled");
}
});
$.post("get.php?op=name", {queryString: ""+thisValue+""}, function(data){
if(data.length >0) {
$('#nameString').val(data);
$('#nameString').attr("disabled","disabled");
}
});
$.post("get.php?op=author", {queryString: ""+thisValue+""}, function(data){
if(data.length >0) {
$('#authorString').val(data);
$('#authorString').attr("disabled","disabled");
}
});
$.post("get.php?op=publisher", {queryString: ""+thisValue+""}, function(data){
if(data.length >0) {
$('#publisherString').val(data);
$('#publisherString').attr("disabled","disabled");
}
});
setTimeout("$('#suggestions').hide();", 200);
}
function res(inputString) {
$('#publisherString').attr("disabled", false);
$('#publisherString').val('');
$('#nameString').attr("disabled",false);
$('#nameString').val('');
$('#authorString').attr("disabled",false);
$('#authorString').val('');
$('#mymenu').attr("disabled",false);
$('#mymenu option').attr('selected', false);
}
Run Code Online (Sandbox Code Playgroud) 我认为这应该很容易.这里有很多关于使用jQuery取消选中复选框的答案但是当我尝试它们时它们似乎不起作用.我想取消选中它们作为重置表单功能的一部分.
我有一个循环,我在其中创建复选框:
<input type="checkbox" name="is_chosen[]" value="{{ $sample->id }}" class="flat">
这是我取消选中复选框的代码(并非最初检查所有复选框):
$('input:checkbox').each(function () {
if(this.checked) {
$(this).attr('checked', false);
}
});
Run Code Online (Sandbox Code Playgroud)
我也曾尝试更换attr用prop.我也尝试过使用$(input:checkbox).prop('checked', false);.我已经尝试过其他人,但他们没有工作.我错过了什么?
jquery ×7
attr ×4
javascript ×4
prop ×4
html ×2
checkbox ×1
deprecated ×1
dom ×1
option ×1
select ×1