相关疑难解决方法(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万
查看次数

在jQuery中设置textarea的值

我试图使用jquery在textarea字段中设置一个值,代码如下:

$("textarea#ExampleMessage").attr("value", result.exampleMessage);
Run Code Online (Sandbox Code Playgroud)

问题是,一旦执行此代码,它不会改变textarea中的文本?

但是,当执行alert($("textarea#ExampleMessage").attr("value"))新设置的值时会返回?

jquery textarea

511
推荐指数
12
解决办法
89万
查看次数

如何使用jquery将输入元素更改为textarea

我有以下html:

<div>
    <input type="text" style="xxxx" size="40"/>
    <input type="text" style="xxxx" size="100"/>
    <input type="text" style="xxxx" size="100"/>
    <input type="text" style="xxxx" size="40"/>
</div>
Run Code Online (Sandbox Code Playgroud)

现在我想将大小为"100"的每个输入更改为与输入具有相同样式的textarea.

我试过这个:

$("input[size=100]").each(function(){
  //how to replace it?
});
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?


我在这里的答案中使用了这个解决方案:

$("input[size=100]").each(function() {
    var style = $(this).attr('style'), textbox = $(document.createElement('textarea')).attr({
        id : $(this).id,
        name : $(this).name,
        value : $(this).val(),
        style : $(this).attr("style"),
        "class" : $(this).attr("class"),
        rows : 6
    }).width($(this).width());
    $(this).replaceWith(textbox);
});
Run Code Online (Sandbox Code Playgroud)

jquery

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

标签 统计

jquery ×3

attr ×1

dom ×1

javascript ×1

prop ×1

textarea ×1