簡體   English   中英

createTextNode不保留字體樣式

[英]createTextNode not keeping font style

所以現在我正在處理一個列表,您可以動態地添加內容。 所以到目前為止我有這段代碼,這是我的javascript

$("#textBox").keypress(function(e) {
if(e.keyCode == 13)
    addToSticky();
});

function addToSticky() {
    if(count < 5){      
        var text = document.getElementById("textBox").value;
        var node = document.createElement("LI");
        var textnode = document.createTextNode(text);
        node.appendChild(textnode);
        document.getElementById("list").appendChild(node);
        count = count + 1;
    }else
        hideBox("textBox");
}

這是我的html

<a href="#" class="sticky" STYLE="text-decoration: none">
    <h2 STYLE="font-size:200%; font-weight:bold; padding-bottom:10px; margin-top: 4px"> 
                Notes 
    </h2>
    <p>                 
        <ul id="list" STYLE="padding-left: 20px">
            <li> <b>Hire this guy! </b></li>
        </ul>
        <input type="text" name="input" id="textBox">               
    </p>
</a>

最后但並非最不重要的一點是,我在CSS中將<a>,<li>和<h2>全部設置為自定義字體。 但是,但是當您輸入要添加到列表中的內容時,它會鍵入自定義字體,但是最后添加到appendChild時,它不再具有自定義字體。

第一個元素的文本包裝在一個粗體標簽內,而后面沒有添加。

如果要使列表項變為粗體,則最好的方法是使用CSS設置所有列表項元素的font-weight屬性:

li {
    font-weight : bold;
}

插入以下行

var font = document.createElement("b");
node.appendChild(font);

var textnode = document.createTextNode(text)

看到http://jsfiddle.net/1e8zzLep/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM