我正在尝试弄清楚如何在点击时将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>
答案 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
即可。请参阅下面的代码段:
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;
但如果可以动态添加元素,请执行以下操作:
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;