英文:
insert string as html when button clicked
问题
var appleImg = "<img src=\"https://codehs.com/uploads/afcd3bf7ea7baf3f7d571c6e24c352af\">";
var pearImg = "<img src=\"https://codehs.com/uploads/e0c442f8c020685fc8016f15c2547f1e\">";
// Add your JavaScript code below
var middleCell = document.getElementById("middleCell");
function fruitTest(){
var myInput = document.getElementById("input1");
var inputValue = myInput.value;
if (inputValue > 100){
middleCell.innerHTML = appleImg;
} else if (inputValue < 0){
middleCell.innerHTML = pearImg;
}
}
function clearBox (){
middleCell.innerHTML = "";
}
英文:
I have the below problem where I am trying to get the code from appleImg
and pearImg
to be put into an html element called middleCell
should a button be clicked and based off a value of an input (input1
). How can I make this work?
<table border="1">
<tr>
<td>
<input id="input1">
<button id="leftButton" onclick="fruitTest();">Enter</button>
</td>
<td>
<p id="middleCell">Hello</p>
</td>
<td>
<button id= "clearButton" onclick="clearBox();">Clear</button>
</td>
</tr>
</table>
var appleImg = "<img src=\"https://codehs.com/uploads/afcd3bf7ea7baf3f7d571c6e24c352af\">";
var pearImg = "<img src=\"https://codehs.com/uploads/e0c442f8c020685fc8016f15c2547f1e\">";
// Add your JavaScript code bel
var middleCell = document.getElementById("middleCell");
function fruitTest(){
var myInput = document.getElementById("input1");
var inputValue = myInput.value;
if (inputValue > 100){
middleCell.innerHtml = appleImg;
} else if (inputValue < 0){
middleCell.innerHtml = pearImg;
}
}
function clearBox (){
middleCell.innerHtml = "";
}
I wanted the value of the middleCell
to show the images based of what was entered. When 101 is entered and button clicked, the appkle
image should show up.
Also, when clicking clear, the middleCell
should be clear.
答案1
得分: -1
在这里进行了一些测试后,我认为如果您需要保留文本和图像,可以使用以下方法:
middleCell.insertAdjacentHTML('afterbegin', IMAGE)
我喜欢这种方法,因为您可以在不同位置插入HTML(即使这样做可能不是所有情况的最佳选择 :p)。
https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML
而不是使用:
middleCell.innerHtml = ...
点击清除按钮后移除图像的方法之一是使用`querySelector`获取添加的图像(https://developer.mozilla.org/pt-BR/docs/Web/API/Document/querySelector):
const image = middleCell.querySelector('img')
image.remove()
英文:
After doing some tests here, I think that you can use the following if you need to keep the text + image:
middleCell.insertAdjacentHTML('afterbegin', IMAGE)
I like this method cause you can insert the HTML (even if doing this can not be the best option for all the cases :p) in different positions
https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML
Instead of doing the
middleCell.innerHtml = ...
And to remove the image after clicking on the clear button, one approach should be getting the added image by using querySelector
(https://developer.mozilla.org/pt-BR/docs/Web/API/Document/querySelector):
const image = middleCell.querySelector('img')
image.remove()
答案2
得分: -1
你应该使用 innerHTML
而不是 innerHtml
,也不建议将元素硬编码为字符串。你可以使用相应的 JavaScript 函数来创建元素。此外,考虑使用 const
和 let
而不是 var
。
请告诉我这是否有所帮助。
英文:
You should use innerHTML
instead of innerHtml
and also I wouldn't recommend to hardcode element as a string. You can create elements using the corresponding JS function instead. Also, consider using const
and let
instead of var
.
Please let me know if this helps.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const appleImg = "https://codehs.com/uploads/afcd3bf7ea7baf3f7d571c6e24c352af";
const pearImg = "https://codehs.com/uploads/e0c442f8c020685fc8016f15c2547f1e";
const middleCell = document.getElementById('middleCell');
function fruitTest(){
const myInput = document.getElementById('input1');
const inputValue = myInput.value;
const img = document.createElement('img');
if (inputValue > 100){
img.src = appleImg;
} else if (inputValue < 0){
img.src = pearImg;
}
middleCell.innerHTML = '';
middleCell.appendChild(img);
}
function clearBox (){
middleCell.innerHTML = '';
}
<!-- language: lang-html -->
<table border="1">
<tr>
<td>
<input id="input1">
<button id="leftButton" onclick="fruitTest();">Enter</button>
</td>
<td>
<p id="middleCell">Hello</p>
</td>
<td>
<button id= "clearButton" onclick="clearBox();">Clear</button>
</td>
</tr>
</table>
<!-- end snippet -->
答案3
得分: -2
I changed innerHtml to innerHTML. The HTML must be in all caps. That looks like what your problem was.
此处将 innerHtml 更改为 innerHTML。HTML 必须全部大写。这似乎是您的问题所在。
Plus I added parseInt() around inputValue to make sure it was converted to a number
此外,我在 inputValue 周围添加了 parseInt(),以确保它被转换为数字
var appleImg = "<img src="https://codehs.com/uploads/afcd3bf7ea7baf3f7d571c6e24c352af">";
var pearImg = "<img src="https://codehs.com/uploads/e0c442f8c020685fc8016f15c2547f1e">;
var middleCell = document.getElementById("middleCell");
function fruitTest(){
var myInput = document.getElementById("input1");
var inputValue = myInput.value;
if (parseInt(inputValue) > 100){
middleCell.innerHTML = appleImg;
} else if (parseInt(inputValue) < 0){
middleCell.innerHTML = pearImg;
}
}
function clearBox (){
middleCell.innerHTML = "";
}
英文:
I changed innerHtml to innerHTML. The HTML must be in all caps. That looks like what your problem was.<br />
Plus I added parseInt() around inputValue to make sure it was converted to a number
var appleImg = "<img src=\"https://codehs.com/uploads/afcd3bf7ea7baf3f7d571c6e24c352af\">";
var pearImg = "<img src=\"https://codehs.com/uploads/e0c442f8c020685fc8016f15c2547f1e\">";
// Add your JavaScript code bel
var middleCell = document.getElementById("middleCell");
function fruitTest(){
var myInput = document.getElementById("input1");
var inputValue = myInput.value;
if (parseInt(inputValue) > 100){
middleCell.innerHTML = appleImg;
} else if (parseInt(inputValue) < 0){
middleCell.innerHTML = pearImg;
}
}
function clearBox (){
middleCell.innerHTML = "";
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论