相关疑难解决方法(0)

.prop()vs .attr()

所以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 jquery dom prop attr

2249
推荐指数
14
解决办法
57万
查看次数

如何将输入字段设为只读但仍然将数据发送回表单?

我有一个输入字段:

<input cid="Topic_Created" name="Topic.Created" size="25" type="text" value="6/5/2011 8:22:45 AM" />
Run Code Online (Sandbox Code Playgroud)

我希望该字段显示在我的表单上,但不希望用户能够编辑该字段.当用户单击提交时,我希望将表单值发送回服务器.

这可能吗.我尝试了不同的组合disabled = "disabled",readonly = "readonly".似乎我总是得不到为该领域送回的任何东西.

html css

27
推荐指数
1
解决办法
6万
查看次数

标签 统计

attr ×1

css ×1

dom ×1

html ×1

javascript ×1

jquery ×1

prop ×1