所以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
,为什么?这对我未来的编码有何影响?
如何在不添加jQuery值的情况下设置数据属性?我要这个:
<body data-body>
Run Code Online (Sandbox Code Playgroud)
我试过了:
$('body').attr('data-body'); // this is a getter, not working
$('body').attr('data-body', null); // not adding anything
Run Code Online (Sandbox Code Playgroud)
其他所有东西似乎都将第二个参数添加为字符串.是否可以只设置一个没有值的属性?
这是我的代码:
$("#product1 :checkbox").click(function(){
$(this)
.closest('tr') // Find the parent row.
.find(":input[type='text']") // Find text elements in that row.
.attr('disabled',false).toggleClass('disabled') // Enable them.
.end() // Go back to the row.
.siblings() // Get its siblings
.find(":input[type='text']") // Find text elements in those rows.
.attr('disabled',true).removeClass('disabled'); // Disable them.
});
Run Code Online (Sandbox Code Playgroud)
我如何切换.attr('disabled',false);
?
我似乎无法在Google上找到它.
如何在按钮点击时通过Javascript 更改标签的href
属性值<a/>
?
<script type="text/javascript">
function f1()
{
document.getElementById("abc").href="xyz.php";
}
</script>
<a href="" id="abc">jhg</a>
<a href="" id="" onclick="f1()">jhhghj</a>
Run Code Online (Sandbox Code Playgroud) 现在,这不只是一个有什么区别的问题,我已经做了一些测试(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/1
2)Attr修改测试
Prop:http://fiddle.jshell.net/test/1
Attr:/test/1
3)Attr然后Prop Propification测试
Prop:http://fiddle.jshell.net/test/11
Attr:http://fiddle.jshell.net/test/11
4)Prop然后Attr修改测试
Prop:http://fiddle.jshell.net/test/11
Attr:http://fiddle.jshell.net/test/11
现在我对一些事情感到困惑,据我所知:
Prop:在通过JavaScript
Attr进行任何修改后的当前状态值:在页面加载的html中定义的值.
现在如果这是正确的,
prop
似乎使action
完全合格,相反为什么修改属性不?prop
in 1)
修改属性,对我来说没有意义?attr
in 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) 我需要删除有的元素value="123"
.我知道所有具有不同值的元素都位于其中#attached_docs
,但我不知道如何选择元素value="123"
.
$('#attached_docs').find ... .remove();
Run Code Online (Sandbox Code Playgroud)
你能帮助我吗?
我创建了一个自定义视图(在此处找到),其中包含enum类型的declare-styleable属性.在xml中,我现在可以为自定义属性选择一个枚举条目.现在我想创建一个以编程方式设置此值的方法,但我无法访问枚举.
attr.xml
<declare-styleable name="IconView">
<attr name="icon" format="enum">
<enum name="enum_name_one" value="0"/>
....
<enum name="enum_name_n" value="666"/>
</attr>
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
layout.xml
<com.xyz.views.IconView
android:id="@+id/heart_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:icon="enum_name_x"/>
Run Code Online (Sandbox Code Playgroud)
我需要的是:mCustomView.setIcon(R.id.enum_name_x);
但是我找不到枚举,或者我甚至不知道如何获得枚举或枚举的名称.
我在一些地方看到.attr()用于jQuery.在一些地方.prop()被使用.但我搜索了SO和谷歌我很困惑.请告诉我这两者之间的确切区别以及何时使用它们.
我已经看到以下链接 jQuery attr与prop,有一个道具列表? jQuery attr vs prop?
但我没有得到答案.请帮帮我.谢谢.
在给出downvote之前请说明原因,然后我将在下一篇文章中更正.
我试图用替换方法更改散列URL(document.location.hash),但它不起作用.
$(function(){
var anchor = document.location.hash;
//this returns me a string value like '#categories'
$('span').click(function(){
$(window).attr('url').replace(anchor,'#food');
//try to change current url.hash '#categories'
//with another string, I stucked here.
});
});
Run Code Online (Sandbox Code Playgroud)
我不想更改/刷新页面,我只想更换URL而不做任何回复.
注意:我不想用href ="#food"解决方案来解决这个问题.
attr ×10
jquery ×7
javascript ×5
prop ×3
android ×2
attributes ×1
custom-view ×1
dom ×1
enums ×1
find ×1
hash ×1
href ×1
replace ×1
toggle ×1