将字符串插入为HTML当按钮被点击。

huangapple go评论89阅读模式
英文:

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?

         &lt;table border=&quot;1&quot;&gt;
            &lt;tr&gt;
                &lt;td&gt;
                 &lt;input id=&quot;input1&quot;&gt;
                 &lt;button id=&quot;leftButton&quot; onclick=&quot;fruitTest();&quot;&gt;Enter&lt;/button&gt;
                &lt;/td&gt;
                &lt;td&gt;
                    &lt;p id=&quot;middleCell&quot;&gt;Hello&lt;/p&gt;
                &lt;/td&gt;
                &lt;td&gt;
                 &lt;button id= &quot;clearButton&quot; onclick=&quot;clearBox();&quot;&gt;Clear&lt;/button&gt;   
                &lt;/td&gt;
            &lt;/tr&gt;
        &lt;/table&gt;
var appleImg = &quot;&lt;img src=\&quot;https://codehs.com/uploads/afcd3bf7ea7baf3f7d571c6e24c352af\&quot;&gt;&quot;;
var pearImg = &quot;&lt;img src=\&quot;https://codehs.com/uploads/e0c442f8c020685fc8016f15c2547f1e\&quot;&gt;&quot;;


// Add your JavaScript code bel
var middleCell = document.getElementById(&quot;middleCell&quot;);
function fruitTest(){
    var myInput = document.getElementById(&quot;input1&quot;);
    var inputValue = myInput.value;
    if (inputValue &gt; 100){
        middleCell.innerHtml = appleImg;
    } else if (inputValue &lt; 0){
        middleCell.innerHtml = pearImg;
    }
} 
function clearBox (){
    middleCell.innerHtml = &quot;&quot;;
}

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(&#39;afterbegin&#39;, 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(&#39;img&#39;)
image.remove()

答案2

得分: -1

你应该使用 innerHTML 而不是 innerHtml,也不建议将元素硬编码为字符串。你可以使用相应的 JavaScript 函数来创建元素。此外,考虑使用 constlet 而不是 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 = &quot;https://codehs.com/uploads/afcd3bf7ea7baf3f7d571c6e24c352af&quot;;
const pearImg = &quot;https://codehs.com/uploads/e0c442f8c020685fc8016f15c2547f1e&quot;;

const middleCell = document.getElementById(&#39;middleCell&#39;);
function fruitTest(){
    const myInput = document.getElementById(&#39;input1&#39;);
    const inputValue = myInput.value;
    const img = document.createElement(&#39;img&#39;);
    if (inputValue &gt; 100){
       img.src = appleImg;
    } else if (inputValue &lt; 0){
       img.src = pearImg;
    }
    middleCell.innerHTML = &#39;&#39;;
    middleCell.appendChild(img);
} 
function clearBox (){
    middleCell.innerHTML = &#39;&#39;;
}

<!-- language: lang-html -->

         &lt;table border=&quot;1&quot;&gt;
            &lt;tr&gt;
                &lt;td&gt;
                 &lt;input id=&quot;input1&quot;&gt;
                 &lt;button id=&quot;leftButton&quot; onclick=&quot;fruitTest();&quot;&gt;Enter&lt;/button&gt;
                &lt;/td&gt;
                &lt;td&gt;
                    &lt;p id=&quot;middleCell&quot;&gt;Hello&lt;/p&gt;
                &lt;/td&gt;
                &lt;td&gt;
                 &lt;button id= &quot;clearButton&quot; onclick=&quot;clearBox();&quot;&gt;Clear&lt;/button&gt;   
                &lt;/td&gt;
            &lt;/tr&gt;
        &lt;/table&gt;

<!-- 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 = &quot;&lt;img src=\&quot;https://codehs.com/uploads/afcd3bf7ea7baf3f7d571c6e24c352af\&quot;&gt;&quot;;
var pearImg = &quot;&lt;img src=\&quot;https://codehs.com/uploads/e0c442f8c020685fc8016f15c2547f1e\&quot;&gt;&quot;;


// Add your JavaScript code bel
var middleCell = document.getElementById(&quot;middleCell&quot;);
function fruitTest(){
    var myInput = document.getElementById(&quot;input1&quot;);
    var inputValue = myInput.value;
    if (parseInt(inputValue) &gt; 100){
       middleCell.innerHTML = appleImg;
    } else if (parseInt(inputValue) &lt; 0){
        middleCell.innerHTML = pearImg;
    }
} 
function clearBox (){
    middleCell.innerHTML = &quot;&quot;;
}

huangapple
  • 本文由 发表于 2023年5月22日 08:33:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定