简体   繁体   English

使用按钮将预定义的html添加到div

[英]Add predefined html to a div with buttons

I'm trying to do a small piece of code that does the following : I have many groups of buttons plus a div, and clicking the buttons adds a predefined text to the matching div. 我正在尝试做一小段代码,该代码可以执行以下操作:我有许多按钮组以及一个div,单击这些按钮会向匹配的div添加预定义的文本。

I put my code as an example here : https://jsfiddle.net/kdpqu98p/3/ 我在这里以代码为例: https//jsfiddle.net/kdpqu98p/3/

HTML 的HTML

<button id="hey-0">hey</button>
<button id="hi-0">hi</button>
<button id="hello-0">hello</button>
<div id="text-0"></div>

<button id="hey-1">hey</button>
<button id="hi-1">hi</button>
<button id="hello-1">hello</button>
<div id="text-1"></div>

<button id="hey-2">hey</button>
<button id="hi-2">hi</button>
<button id="hello-2">hello</button>
<div id="text-2"></div>

JAVASCRIPT JAVASCRIPT

$(document).ready(function() {

  // Loops through all 3 groups
  for (var i = 2; i >= 0; i--) {
    // Gets the buttons and text block for this group.
    var text = '#text-' + i;
    var hey = '#hey-' + i;
    var hi = '#hi-' + i;
    var hello = '#hello-' + i;

    // Add functions to the buttons.
    $(hey).click(function(e) {
      $(text).append('hey');
    });

    $(hi).click(function(e) {
      $(text).append('hi');
    });

    $(hello).click(function(e) {
      $(text).append('hello');
    });
  }

});

It almost works as I want it to, but it always adds the text to the first div and not the one corresponding to the buttons, because... reasons. 它几乎可以按我想要的方式工作,但是它总是将文本添加到第一个div而不是与按钮相对应的文本,因为...原因。 Oo o

So here are my questions : First, why does finding the right button works, but not for the div (since all buttons work but it always adds the text to the first div). 所以这是我的问题:首先,为什么找到正确的按钮有效,但对div无效(因为所有按钮均有效,但总是将文本添加到第一个div)。 Then, is there an even easier and faster way to do that? 然后,有没有更简单,更快捷的方法来做到这一点? I'm pretty new to javascript and jquery so you'll have to speak like to a 3 year old to me. 我对javascript和jquery还是很陌生,所以您必须像3岁的孩子一样对我说话。 I'm sure there is a way to get rid of the loop and use only one function that will say something like "for all (#/word/-/index/) buttons, make them add the /word/ to the html of the #text-/index/ div" but I have no clue how to do it. 我敢肯定,有一种方法可以摆脱循环,只使用一个函数,它会对所有(#/ word /-// index /)按钮说类似的东西,使它们将/ word /添加到HTML的#text- / index / div”,但我不知道如何执行此操作。

Thanks a lot for your answers ! 非常感谢你的回答 !

You can massively simplify your code by using DRY principles. 您可以使用DRY原理来大大简化代码。 Firstly put a common class on all the button elements, and use the value attribute to store the value to be placed in the related div . 首先,在所有button元素上放置一个通用类,并使用value属性存储要放入相关div From there you can use a single event handler on that class which finds the related div and adds the value. 从那里,您可以在该类上使用单个事件处理程序,该处理程序查找相关的div并添加值。 Try this: 尝试这个:

 $('.btn').click(function() { $(this).nextAll('.text:first').append(this.value); }); 
 div { width: 300px; height: 40px; overflow-x: scroll; margin-bottom: 10px; background-color: #efefef; border: 1px solid black; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="btn" value="hey-0">hey</button> <button class="btn" value="hi-0">hi</button> <button class="btn" value="hello-0">hello</button> <div class="text"></div> <button class="btn" value="hey-1">hey</button> <button class="btn" value="hi-1">hi</button> <button class="btn" value="hello-1">hello</button> <div class="text"></div> <button class="btn" value="hey-2">hey</button> <button class="btn" value="hi-2">hi</button> <button class="btn" value="hello-2">hello</button> <div class="text"></div> 

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

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