所以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,为什么?这对我未来的编码有何影响?
出于某种原因,我似乎无法弄清楚这一点.
我的html中有一些单选按钮可以切换类别:
<input type="radio" name="main-categories" id="_1234" value="1234" /> // All
<input type="radio" name="main-categories" id="_2345" value="2345" /> // Certain category
<input type="radio" name="main-categories" id="_3456" value="3456" /> // Certain category
<input type="radio" name="main-categories" id="_4567" value="4567" /> // Certain category
Run Code Online (Sandbox Code Playgroud)
用户可以选择他/她想要的任何一个,但是当某个事件触发时,我想设置1234为设置选中的单选按钮,因为这是默认选中的单选按钮.
我尝试了这个版本(有和没有jQuery):
document.getElementById('#_1234').checked = true;
但它似乎没有更新.我需要它明显更新,以便用户可以看到它.有人可以帮忙吗?
编辑:我只是累了,忽略了#,谢谢你指出,那和$.prop().
我正在使用Bootstrap按钮作为单选按钮。 http://getbootstrap.com/javascript/#buttons
这是我当前的代码:
<div class="btn-group col-md-2" data-toggle="buttons">
<label class="btn btn-gender btn-default active">
<input type="radio" id="gender" name="gender" value="female"> Girls
</label>
<label class="btn btn-gender btn-default">
<input type="radio" id="gender" name="gender" value="male"> Guys
</label>
</div>
Run Code Online (Sandbox Code Playgroud)
如何使用JavaScript设置“男性”进行检查?
可能的重复:
如何使用 jQuery 检查单选按钮?
如何更改单选按钮值。
jQuery('input:radio[name=search][id=search-damages]').checked = true;
Run Code Online (Sandbox Code Playgroud)
我试过这个,但它不起作用
<input type="radio" name="search" id="search-vehicle" value="search-vehicle" checked>
<input type="radio" name="search" id="search-damages" value="search-damages">
Run Code Online (Sandbox Code Playgroud) 我是JavaScript的初学者.我的动态页面上有几个单选按钮,我想创建一个脚本来进行以下操作:
HTML:
<input type="radio" id="elemainfoto">
<input type="radio" id="elemainfoto">
<input type="radio" id="elemainfoto">
Run Code Online (Sandbox Code Playgroud)
JavaScript的:
var radio = '#elemainfoto',
if(radd.value == 0) {
radd.checked the first radio element,
} else {
keep the way it is,
}
Run Code Online (Sandbox Code Playgroud)
如果没有标记任何无线电元素,则标记第一个必须的.
使用这个jquery代码:
$("#targeting input:radio").each(function() {
console.log ($(this).attr("selected"));
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<input type="radio" id="dateFromTo" name="muScheduleDateRange" value="2" selected="someval"/>
Run Code Online (Sandbox Code Playgroud)
在console.log中,我正在selected改为someval?