单击时移动按钮并替换为文本

时间:2018-06-13 03:38:34

标签: javascript css

我正在尝试弄清楚如何在点击时将JavaScript中的文本添加到“button2”正上方的页面。我尝试了不同命令的组合,但无法做到正确。

var theButton = {

  "facts": [
  {
      "Size": "250 pixels",
      "Color": "Black",
      "Font": "Times New Roman",
    }

  ]
};


var largeButton = document.getElementById("button2");
var buttonItems = theButton.facts.length;

largeButton.addEventListener("click", text, false);

function text() {

  for (i = 0; i < buttonItems; i++) {

    largeButton.insertAdjacentHTML("beforebegin", "Size: " + theButton.facts[i].Size + 
    '<br />' + "Color: " + theButton.facts[i].Color +
      '<br />' + "Font: " + theButton.facts[i].Font)
  }
};
button {
  height: 100px;
  width: 100px;
  background-color: black;
  font-family: Times New Roman;
  color: white;
}

#button1 {
  float: left;
}

#button2 {
  float: right;
}
<button id="button1">Button</button>

<button id="button2">Button2</button>

2 个答案:

答案 0 :(得分:2)

尝试这样

var theButton = {

  "facts": [{
      "Size": "250 pixels",
      "Color": "Black",
      "Font": "Times New Roman",
    }

  ]
};


var largeButton = document.getElementById("button2");
var buttonItems = theButton.facts.length;

largeButton.addEventListener("click", text, false);

function text() {

  for (i = 0; i < buttonItems; i++) {

    largeButton.insertAdjacentHTML("beforebegin", "<p>Size: " + theButton.facts[i].Size +
      '<br />' + "Color: " + theButton.facts[i].Color +
      '<br />' + "Font: " + theButton.facts[i].Font) + "</p>"
  }
};
button {
  height: 100px;
  width: 100px;
  background-color: black;
  font-family: Times New Roman;
  color: white;
}

.first {
  margin-right: auto;
}

.second {
  margin-left: auto;
}
<div style="display: flex;">
  <div class="first">
    <button id="button1">Button</button>
  </div>
  <div class="second">
    <button id="button2">Button2</button>
  </div>
</div>

var theButton = {

  "facts": [{
      "Size": "250 pixels",
      "Color": "Black",
      "Font": "Times New Roman",
    }

  ]
};


var largeButton = document.getElementById("button2");
var buttonItems = theButton.facts.length;

largeButton.addEventListener("click", text, false);

function text() {

  for (i = 0; i < buttonItems; i++) {

    largeButton.insertAdjacentHTML("beforebegin", "<div style='float: right;'>Size: " + theButton.facts[i].Size +
      '<br />' + "Color: " + theButton.facts[i].Color +
      '<br />' + "Font: " + theButton.facts[i].Font) + "</div>";
    largeButton.insertAdjacentHTML("beforebegin", "<div class='clearfix'> </div>");
  }

};
button {
  height: 100px;
  width: 100px;
  background-color: black;
  font-family: Times New Roman;
  color: white;
}

#button1 {
  float: left;
}

#button2 {
  float: right;
}

.clearfix::after {
  content: "";
  clear: both;
  display: table;
}
<button id="button1">Button</button>
<button id="button2">Button2</button>

答案 1 :(得分:0)

为了做到这一点而不弄乱HTML,您只需将direction父元素的button2更改为rtl即可。请参阅下面的代码段:

&#13;
&#13;
var theButton = { 'facts': [ { 'Size': '250 pixels', 'Color': 'Black', 'Font': 'Times New Roman' } ] },
    largeButton = document.getElementById( 'button2' ),
    buttonItems = theButton.facts.length;

largeButton.addEventListener( 'click', function() {
    for ( i = 0, html = ''; i < buttonItems; i++ ) {
        html = 'Size: ' +
            theButton.facts[i].Size +' <br>Color: ' +
            theButton.facts[i].Color + '<br>Font: ' +
            theButton.facts[i].Font + '<br>'
    }

    largeButton.insertAdjacentHTML( 'beforebegin', html );
}, false )
&#13;
body {
    direction: rtl
}
button {
    height: 100px;
    width: 100px;
    background-color: black;
    font-family: Times New Roman;
    color: white
}
#button1 {
    float: left
}
#button2 {
    float: right
}
&#13;
<button id="button1">Button</button>
<button id="button2">Button2</button>
&#13;
&#13;
&#13;

但如果可以动态添加元素,请执行以下操作:

&#13;
&#13;
var theButton = { 'facts': [ { 'Size': '250 pixels', 'Color': 'Black', 'Font': 'Times New Roman' } ] },
    largeButton = document.getElementById( 'button2' ),
    buttonItems = theButton.facts.length;

largeButton.addEventListener( 'click', function() {
    for ( i = 0, html = ''; i < buttonItems; i++ ) {
        html = 'Size: ' +
            theButton.facts[i].Size +' <br>Color: ' +
            theButton.facts[i].Color + '<br>Font: ' +
            theButton.facts[i].Font + '<br>'
    }

    var newPel = document.getElementById( 'new' );

    if ( !newPel ) {
        largeButton.insertAdjacentHTML( 'beforebegin', '<p id="new"></p>' );
        newPel = document.getElementById( 'new' )
    }

    newPel.appendChild( largeButton );
    largeButton.insertAdjacentHTML( 'beforebegin', html );
}, false )
&#13;
button {
    height: 100px;
    width: 100px;
    background-color: black;
    font-family: Times New Roman;
    color: white
}
#button1 {
    float: left
}
#button2, p {
    float: right
}
&#13;
<button id="button1">Button</button>
<button id="button2">Button2</button>
&#13;
&#13;
&#13;