0

我目前有一个按钮,可以将它使用的 textarea 中的文本设为粗体

<a href="#" class="stylingButtons" type="button" onclick="boldTextArea()" value="Bold Text"}>
 <img alt='Bold Button' src='BButton.gif' border='4' width="90px" >
</a>

对于 HTML 部分和

function boldTextArea(){
document.getElementById("TextArea").style.fontWeight="bolder";;
}

对于 javascript

无论如何我可以编辑此编码,以便一旦文本区域中的文本已经是粗体,我可以再次单击以使其变为非粗体?我真的不热衷于使用 jQuery 的想法,因为我从未有过任何使用它的经验,但我愿意接受这种格式的建议吗?

4

2 回答 2

1

如果你不想使用 jquery,这里有一个 quick'n'dirty javascript

function boldText(){
    var target = document.getElementById("TextArea");
    if( target.style.fontWeight == "bolder" ) {
        target.style.fontWeight = "normal";
    } else {
        target.style.fontWeight = "bolder";
    }
}

应该可以正常工作。我猜这是从粗体切换到正常并返回的基本方式。

还要注意双“;” 在你的 JS 中。

希望这可以帮助。

优雅


更新

这是每次单击时更改(切换)文本大小(或任何其他 css 属性)的 jQuery 版本。

$("#textboxid").click( function(e) {
    if( $(this).css("font-size") == "12px" ) {
        $(this).css("font-size", "14px")
    } else {
        $(this).css("font-size", "12px")
    }
});

如果要更改任何其他属性,请将 css 属性替换为您喜欢的。

于 2013-02-11T00:55:39.420 回答
0

我刚刚在我的网站上做了这个,并在这里找到了答案:

按下按钮“加粗”并再次按下“取消加粗”

希望这可以帮助。

于 2013-02-11T00:47:35.707 回答