0

有以下JQuery代码:

    $(document).ready(function(){
        $(".translated").mouseenter(function(){
            $(this).html("<input type='text'/>");
        });
    });

还有一张桌子,里面有很多<div class="translated">带有文字的项目。我需要通过 mouseenter 事件替换 div 块中的文本以输入项目。有用。但是我在替换的过程中还需要为输入元素设置属性,我不知道该怎么做,因为我是JS/JQuery的新手。请给我一些信息。先感谢您。

更新:对不起,我的属性也是用 JS 计算的,它不是恒定的。

更新 2:算法:1)在 div 项中插入输入项 2)更改新插入项的高度

4

3 回答 3

3

只需设置其余的输入属性,type="text"如下所示,

$(this).html("<input type='text' /* other attributes */ />");

或像下面这样使用,

$('<input />').attr({type: 'text'/*, more attributes*/});
于 2012-11-29T19:31:46.320 回答
1
 var someVariable = 1 + 2;
 $(this).html("<input type='text' anotherAttribute='something' attrFromValue='" 
     + someVariable + "' />");
于 2012-11-29T19:33:10.337 回答
0

作为记录,元素创建有一个特殊的 jQuery 语法。

$("<input/>", {
    type: "text",
    height: 100
}).appendTo(this);

上面插入了一个属性设置为且(CSS) 设置为的input元素。typetextheight100px

附加到目标元素的生成标记是:

<input type="text" style="height: 100px;">
于 2012-11-29T19:43:06.073 回答