所以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,为什么?这对我未来的编码有何影响?
现在,这不只是一个有什么区别的问题,我已经做了一些测试(http://jsfiddle.net/ZC3Lf/)修改prop和attr的<form action="/test/"></form>? 与输出是:
1)prop修改测试
Prop:http://fiddle.jshell.net/test/1
Attr:http://fiddle.jshell.net/test/12)Attr修改测试
Prop:http://fiddle.jshell.net/test/1
Attr:/test/13)Attr然后Prop Propification测试
Prop:http://fiddle.jshell.net/test/11
Attr:http://fiddle.jshell.net/test/114)Prop然后Attr修改测试
Prop:http://fiddle.jshell.net/test/11
Attr:http://fiddle.jshell.net/test/11
现在我对一些事情感到困惑,据我所知:
Prop:在通过JavaScript
Attr进行任何修改后的当前状态值:在页面加载的html中定义的值.
现在如果这是正确的,
prop似乎使action完全合格,相反为什么修改属性不?propin 1)修改属性,对我来说没有意义?attrin 2)修改属性,它们是否意味着以这种方式链接?HTML
JavaScript的
var element = $('form');
var property = 'action';
/*You should not need to modify below this line */
var body = …Run Code Online (Sandbox Code Playgroud) 是idHTML的属性或属性?
我应该怎么做$('#selector').attr('id');或$('#selector').prop('id');
我读了很多文章,我仍然感到困惑.
有人可以向我解释一下HTML/JS中属性和属性之间的差异是非常简单的语言吗?
$('#operatordelivery').attr('checked', true);
Run Code Online (Sandbox Code Playgroud)
嗨,我目前正致力于将jQuery版本迁移到jQuery-2.1.1,我可以在控制台中看到警告JQMIGRATE: jQuery.fn.attr('selected') may use property instead of attribute.我没有清楚地知道这个警告解释了什么.有人能告诉我这个错误是什么意思吗?
我有一个MVC视图,上面有许多按钮(篮子里的每个项目都有2个按钮)......
<button class="pbbg" id="ProductMinus_161637" type="button">-</button>
<button class="pbbg" id="ProductPlus_161637" type="button">+</button>
Run Code Online (Sandbox Code Playgroud)
(他们都有一个onclick活动)
当按下这些按钮中的任何一个时,我想禁用每个产品的所有按钮,直到篮子完成更新.
click事件调用JavaScript函数,而JavaScript函数又调用Ajax.在这篇文章之后,我尝试做的第一件事是禁用所有按钮.....
$("input[type=button]").attr("disabled", "disabled");
Run Code Online (Sandbox Code Playgroud)
然后在Ajax调用返回后重新启用它们....
$("input[type=button]").removeAttr("disabled");
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 产生一些影响或风险的事物即可。
关闭后编辑:请单击下面的重新打开链接以接受此编辑的问题文本。