所以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,为什么?这对我未来的编码有何影响?
如何使用javascript从HTML输入中删除"禁用"属性?
<input id="edit" disabled>
Run Code Online (Sandbox Code Playgroud)
在onClick我希望我的输入标签不包含"禁用"属性.
如何在我的HTML表单中启用提交按钮,如果未选中复选框则禁用?
这段代码有什么问题?
EnableSubmit = function(val)
{
var sbmt = document.getElementById("Accept");
if (val.checked == true)
{
sbmt.disabled = false;
}
else
{
sbmt.disabled = true;
}
}
Run Code Online (Sandbox Code Playgroud)
复选框
<td width="30%">Do you accept Terms of Service agreement?</td>
<td width="10px" style="min-width: 10px"></td>
<td width="70%">
<input type="checkbox" name="TOS" value="Accept" onClick="EnableSubmit"> I agree to Terms of Service agreement.
</td>
Run Code Online (Sandbox Code Playgroud) 我有一个默认情况下未选中的复选框,默认情况下禁用输入.
<label class="checkbox span3"><input type="checkbox"> I am a full-time student.</label>
<input class="inputIcon span3" id="disabledInput" type="text" placeholder="Enter School Name" disabled>
Run Code Online (Sandbox Code Playgroud)
我完全控制了类和id名称,并且该站点使用jQuery,因此可以根据需要使用它或普通的javascript.
如果用户选中该框,则应从以下输入中删除"已禁用"属性.如果用户取消选中,则应再次禁用.
在StackOverflow上发现了几个类似的问题,但似乎没有一个确切的用例.
javascript ×4
forms ×2
jquery ×2
attr ×1
attributes ×1
dom ×1
html ×1
input ×1
prop ×1
validation ×1