简体   繁体   English

使用document.execCommand()使文本变为粗体使页面刷新

[英]making text bold with document.execCommand() makes page refresh

What I am trying to do 我要做什么

I am trying to make some text bold when I click the bold button. 单击粗体按钮时,我试图使某些文本变为粗体。 This triggers a function which calls the execCommand function... unfortunately when I click the button the whole page refreshed and I lose all my text inside the iframe. 这会触发一个调用execCommand函数的函数...不幸的是,当我单击按钮时,整个页面都刷新了,并且我在iframe中丢失了所有文本。

I can see the text go bold for a split second and then the page reloads. 我可以看到文本变为黑体,然后重新加载页面。

My Code 我的密码

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <!-- Latest compiled and minified CSS -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
        <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
    </head>
    <body onLoad="enableEditMode();">

        <div class="container">
            <div class="row">
                <div class="col-md-6 col-md-offset-3">
                    <h2>My Text Editor</h2>

                    <form class="" action="index.html" method="post">

                        <div class="form-group">
                            <label for="title">Title</label>
                            <input class="form-control" type="title" name="title" value="">
                        </div>

                        <div class="form-group">

                            <div class="" id="toolbar">
                                <button onClick="execCmd('bold')"><i class="fa fa-bold"></i></button>
                            </div>
                            <iframe class="form-control" src="" width="" height="" name="richTextField" style="width:100%;height:100%"></iframe>

                        </div>

                        <button class="btn btn-default btn-block" type="submit" name="button">Go!</button>
                    </form>
                </div>
            </div>
        </div>

        <script>
            /**
             *
             */
            function enableEditMode(){
                richTextField.document.designMode = 'On';
            }

            /**
             *
             */
            function execCmd(command){
                richTextField.document.execCommand(command, false, null);
            }
        </script>

    </body>
</html>

My Question 我的问题

How do I make my text bold here and preserve my text? 如何在此处使文本加粗并保存文本?

找到了我的答案:只需在按钮上添加一种类型,以防止它们自动提交表单。

<button onClick="execCmd('bold')" type="button"><i class="fa fa-bold"></i></button>

I think the use is wrong you could firstly set a variable 我认为使用是错误的,您可以先设置一个变量

var rtField = document.getElemetByID('richTextField')

please not in the function ! 请不要在功能上! than add an eventlistener to the button which changes the text 比在按钮上添加事件监听器来更改文本

 var btn = document.getElementByID('button')
 btn.addEventListener('click', function (event) {
     rtField.style.fontWeight = 'bold';
 }

should work. 应该管用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM