为ShowText内部的文本着色以及调用变量吗?

时间:2018-10-29 02:07:02

标签: javascript

下面是我通过一些搜索引用的代码。 showText通过按钮切换显示在页面上的容器中。我尽力为我的问题找到解决方案,但不幸的是我什么都没找到。

我试图给showText内的文本着色,并调用输入变量。每次尝试尝试时,它只会将其作为评论输出。我认为有不同的方法可以做到这一点,但我仍在尝试学习JS。咨询会有所帮助!

$('#gbutton').click(function () {
    $("#gjumbo").slideDown("slow");
    $("#gjumbo").slideDown(10000);
    $("#gjumbo").remove();
});

if (!$('#gbutton').click(function () {
    $("#acontainer").slideDown("slow");
    $("#acontainer").slideDown(10000);
})) {
}


 var showText = function (target, message, index, interval) {
        if (index < message.length) {
            $(target).append(message[index++]);
    //begin edit------------------------------|
            if (message[index] == '\n') {
                $(target).append("<br />");
            }
    //end edit--------------------------------|
            setTimeout(function () {
                showText(target, message, index, interval);
            }, interval);
        }
    };

    $(function () {
        showText("#msg", "test\nTest", 0, 20);
    });

编辑:添加了HTML。

<!-- Start-->
    <div id="acontainer" class="container">
        <div>
            <div class="jumbotron jumbotron-fluid">
                <div class="container">
                    <h1 class="display-4 text-center">Title</h1>
                   <div id="console" class="container">
                       <div class="container">
                           <div class="p-5" id="msg">
                           </div>
                       </div>
                   </div>
                </div>
            </div>
        </div>
    </div>
    <!-- End-->

 <div id="gjumbo" class="jumbotron jumbotron-fluid bg-light">
        <div class="container">
            <h3 class="display-4 text-center">H3 Title</h3>
            <p class="lead">Sample Paragraph</p>
            <div class="input-group mb-3">
                <div class="input-group-prepend">
                    <span class="input-group-text fas fa-user" id="user"></span>
                </div>
                <input type="text" class="form-control" name="user" aria-label="Username or Email"
                       aria-describedby="basic-addon1" required placeholder="Username or Email">
            </div>
        </div>
        <hr class="my-4">
        <p>Sample Paragraph</p>
        <form>
            <div class="form-group">
                <label for="formControlRange">Example Range input</label>
                <input type="range" class="form-control-range" id="formControlRange">
            </div>
            <button id="gbutton" type="button" class="btn btn-dark btn-lg">Button</button>
        </form>
    </div>
</div>

2 个答案:

答案 0 :(得分:0)

基于您的jsfiddle,我想您想执行以下操作:

1)加载用户列表,并将其显示在<div>中。

2)按索引突出显示特定用户

3)单击从(1)加载的用户时,自动填写用户名<input type="text">

如果是,那么您可以参考下面的代码,我简化了功能,然后可以将其移至您的代码中。

function loadData(target,data){
	$(target).append(data)
}

//function colorUser(target,index){
//  let item = $(target + " #user" +index)
//  item.addClass("color")
//}

loadData("#msg","<div id='user1'>User 1</div><div id='user2'>User 2</div><div id='user3'>User 3</div><div id='user4'>User 4</div><div>User 5</div>")

//colorUser("#msg",2)

$("#msg div").on("click",(e)=>{
  $("#username").val(e.target.innerHTML)
})

$("#username").on("keyup",(e)=>{
  $("#fromInput").html(e.target.value)
})
<style>
#msg div:hover{
  color:gray;
  cursor:pointer;
}

#msg div{
  color:blue;
}
</style>

<input type="text" id="username" placeholder="username">
<hr>

<div id="msg">
<h3>User Name List </h3>
<div id="fromInput"></div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>

答案 1 :(得分:0)

我将您的showText功能更改为按单词而不是字母显示(慢速)。使用split("\n")将用新行分隔的字符串拆分为数组

var username,
  userFound,
  userLists = "Testing\nTesting\nTesting\nONIS\nTesting\nTesting\nTesting".split('\n');

$('#gbutton').click(function() {
  userFound = false;
  $("#msg").html('');
  username = $('input[name=user]').eq(0).val();
  if (!username) {
    alert('username empty');
    return
  }
  showText("#msg", userLists, 0, 300);
});
var showText = function(target, message, index, interval) {
  if (index < message.length) {
    // begin coloring username
    if (message[index] == username) {
      $(target).append('<span style="color:yellow">' + message[index++] + '</span><br>');
      userFound = true;
    }
    // end coloring
    else {
      $(target).append(message[index++] + '<br>');
    }
    
    if (index >= userLists.length) {
      if (!userFound)
        $(target).append('<span style="color:yellow;font-weight:bold">Username Not found</span><br>');
      return;
    }
    setTimeout(function() {
      showText(target, message, index, interval);
    }, interval);
  }
};
div#console {
  background-color: #3e3d3d;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div id="console" class="container">
  <div class="container">
    <div class="p-5" id="msg">
    </div>
  </div>
</div>

<input type="text" class="form-control" name="user" aria-label="Username" aria-describedby="basic-addon1" required placeholder="Username" value="ONIS">
<button id="gbutton" type="button" class="btn btn-dark btn-lg">Button</button>