创建自己的div

时间:2016-10-24 07:44:57

标签: javascript jquery

我是JavaScriptjQuery的新手,我有以下代码:

 $("input[type = submit]").click(function(){
            $("<div/>").html(itemInput.value).appendTo(".div1");
            clearInput();
 })

我想知道是否有办法来个性化&#34; &#34; div&#34;在第2行。比如给它一个彩色背景或更改字体?

4 个答案:

答案 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();

 })

阅读更多:

http://www.w3schools.com/jquery/jquery_css.asp

答案 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完全忽略尾部斜杠,标记本身决定它是否自动关闭。