添加if函数以检查随机化函数选择的单词组

时间:2017-08-11 08:02:29

标签: javascript html

我创建了一个工具,可以自动格式化粗体和下划线文本的一些随机段落。我现在的问题是,有时候我会想要阻止单词加下划线和粗体。我的第一个函数选择1-5个单词之间的随机文本段落,并使它们变粗,这很好。下一个函数定义了现在部分粗体文本中的另一组单词。在这里,我想添加我的if功能。它应检查下划线函数选择的单词是否已经是粗体,如果是,则应使下划线函数重复选择单词的步骤,直到找到非粗体的单词。

以下是我正在使用的脚本:

$(document).ready(function() {
$(function() {
    function bold() {
        var target = document.getElementById("randomText");
        var oldHTML = target.innerHTML;
        for (j = 0; j < 25; j++) {
            target = document.getElementById("randomText");
            oldHTML = target.innerHTML;
            for (j = 0; j < 25; j++) {
                words = oldHTML.split(" ");
                wordCount = words.length;
                var newHTML = "";
                var highlightStart = Math.floor(Math.random() * wordCount) + 1;
                var highlightLength = Math.floor(Math.random() * 5) + 1;
                for (i = 0; i < wordCount; i++) {
                    if (i == highlightStart) newHTML = newHTML + "<b>";
                    newHTML = newHTML + words[i] + " ";
                    if (i == highlightStart + highlightLength) newHTML = newHTML + "</b>";
                    target.innerHTML = newHTML;
                }
                oldHTML = newHTML;
            }
        };
    };

    function underline() {
        var target = document.getElementById("randomText");
        var oldHTML = target.innerHTML;
        for (j = 0; j < 25; j++) {
            target = document.getElementById("randomText");
            oldHTML = target.innerHTML;
            for (j = 0; j < 25; j++) {
                words = oldHTML.split(" ");
                wordCount = words.length;
                var newHTML = "";
                var highlightStart = Math.floor(Math.random() * wordCount) + 1;
                var highlightLength = Math.floor(Math.random() * 5) + 1;
                for (i = 0; i < wordCount; i++) {
                    if (i == highlightStart) newHTML = newHTML + "<u>";
                    newHTML = newHTML + words[i] + " ";
                    if (i == highlightStart + highlightLength) newHTML = newHTML + "</u>";
                    target.innerHTML = newHTML;
                }
                oldHTML = newHTML;
            }
        };
    };

    $('#randomText').load('hier.txt', function () { bold(); underline(); });
})



});

1 个答案:

答案 0 :(得分:0)

我想如果用粗体标记<b>word_to_be_bolded</b>创建每个单词会更好,那么你可以使用字符串查找函数检查underline()函数中for循环的开头并递归调用再次underline()功能。

以下是所请求的代码:

    $(document).ready(function() {
      $(function() {
        function bold() {
            var target = document.getElementById("randomText");
            var oldHTML = target.innerHTML;
            for (j = 0; j < 25; j++) {
                target = document.getElementById("randomText");
                oldHTML = target.innerHTML;
                for (j = 0; j < 25; j++) {
                    words = oldHTML.split(" ");
                    wordCount = words.length;
                    var newHTML = "";
                    var highlightStart = Math.floor(Math.random() * wordCount) + 1;
                    var highlightLength = Math.floor(Math.random() * 5) + 1;
                    for (i = 0; i < wordCount; i++) {
                        var bolded_word = "<b>"+words[i]+"</b>";
                        newHTML = newHTML + bolded_word + " ";
                        if (i == highlightStart + highlightLength){
                            target.innerHTML = newHTML;
                        }
                    }
                    oldHTML = newHTML;
                }
            }
        }

        function underline() {
            var target = document.getElementById("randomText");
            var oldHTML = target.innerHTML;
            for (j = 0; j < 25; j++) {
                target = document.getElementById("randomText");
                oldHTML = target.innerHTML;
                for (j = 0; j < 25; j++) {
                    words = oldHTML.split(" ");
                    wordCount = words.length;
                    var newHTML = "";
                    var highlightStart = Math.floor(Math.random() * wordCount) + 1;
                    var highlightLength = Math.floor(Math.random() * 5) + 1;
                    for (i = 0; i < wordCount; i++) {
                        if(words[i].contains("<b>") > 0 || words[i].contains("</b>") > 0){
                            underline();
                        }
                        else{
                            if (i == highlightStart) newHTML = newHTML + "<u>";
                            newHTML = newHTML + words[i] + " ";
                        }
                        if (i == highlightStart + highlightLength) newHTML = newHTML + "</u>";
                        target.innerHTML = newHTML;
                    }
                    oldHTML = newHTML;
                }
            }
        }


        $('#randomText').load('hier.txt', function () { bold(); underline(); });
      })
    }); 

希望这有帮助。