我是JavaScript
和jQuery
的新手,我有以下代码:
$("input[type = submit]").click(function(){
$("<div/>").html(itemInput.value).appendTo(".div1");
clearInput();
})
我想知道是否有办法来个性化&#34; &#34; div&#34;在第2行。比如给它一个彩色背景或更改字体?
答案 0 :(得分:2)
//a/b[count(*) = 1 and count(name) = 1]/../@id
答案 1 :(得分:0)
当然,您可以为其添加一个类,然后在单独的样式表中更改css,或者在jQuery中使用.css方法:
$("input[type = submit]").click(function(){
$("<div/>").css({
'background-color': '#BADA55',
'font-family': 'Helvetica, Arial, sans-serif',
}).html(itemInput.value).appendTo(".div1");
clearInput();
})
答案 2 :(得分:0)
将jQuery .css()
方法与您要设置样式的div一起使用。
$("input[type = submit]").click(function(){
$("<div/>").css({
'color' : 'white',
'background-color' : 'black'
}).html(itemInput.value).appendTo(".div1");
clearInput();
})
阅读更多:
答案 3 :(得分:0)
所有这些答案都是以编程方式进行的 - 您始终可以在创建时简单地传递简单的html -
$("input[type = submit]").click(function(){
$("<div style='color:red;background:blue' class='something'></div>")
.html(itemInput.value).appendTo(".div1");
clearInput();
})
正如仅供参考 - <div/>
并非完全有效 - 您应该使用普通<div>
或<div></div>
- 无论哪种方式,都不要将折旧的XHTML代码放在您最终可能会在源代码中执行此操作:HTML5完全忽略尾部斜杠,标记本身决定它是否自动关闭。