【问题标题】:Changing different attributes of textarea更改 textarea 的不同属性
【发布时间】:2014-03-26 21:15:49
【问题描述】:

我正在尝试制作一个文本区域,我可以在其中将里面的文本更改为粗体、斜体、下划线等... 到目前为止,这是我的代码:

<body>
<form name="form1" action="" method="">
<div id="theForm">
<br><br>
<textarea id="theText" cols="40" rows="5" name="">
</textarea>
<br><br>


</div>
</form>
<button form="theForm" onclick="boldText()" type="button">Bold</button>
<button form="theForm" onclick="italicText()" type="button">Italic</button>
<button form="theForm" onclick="underlineText()" type="button">Underline</button>
</body>
</html>
<style>
#theForm
{ 
text-align:center;
position:absolute;
top:10%;
left:40%;
}
</style>

<script>
function boldtext() {
    theText.style.fontWeight = 'bold';
}
</script>

我什至不确定如何启动 Javascript 代码.. 任何帮助将不胜感激!

【问题讨论】:

    标签: javascript text attributes formatting textarea


    【解决方案1】:

    代码中的文本未定义,您需要获取元素并将其分配给变量

    window.onload = function() {
        theText = document.getElementById('theText');
    };
    

    这将创建全局 theText 变量。并且你的函数需要是boldText(大写文本)。

    【讨论】:

    • 这对添加一些不同的代码很有帮助...谢谢!
    【解决方案2】:

    你可以像使用内联 CSS

    <textarea style="font-weight: bold">My Boldie</textarea>
    

    【讨论】:

    • 如果你使用代码缩进4个空格或者点击{}图标。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    • 2013-12-07
    • 2018-11-30
    • 1970-01-01
    相关资源
    最近更新 更多