gpt4 book ai didi

javascript - 我可以通过 Javascript 函数将图像添加到 HTML 正文吗?

转载 作者:行者123 更新时间:2023-12-03 08:47:46 25 4
gpt4 key购买 nike

当总值达到 10 时,我尝试将图像添加到 HTML 正文中。我尝试从图像初始化的行调用函数,其中宽度和高度返回为 0,除非该值为 10。但是在稍微调整这些行之后,我只能在页面加载时显示或不显示图像,而不能在页面加载后添加图像。我是 HTML 和 Javascript 新手,所以我还不太清楚它们是如何协同工作的。

<html>
<head><title>
Number Builder JavaScript
</title>
<script>
a1 = new Array (
'field1', 'field2', 'field3', 'field4', 'field5', 'field6', 'field7', 'field8' )

a2 = new Array(
5, 10, 15, 100, 245, 300, 500, 750 )

function compute() {
total = 0
for ( i=0; i<a2.length; i++ ) {
if ( document.NumberBuilder[a1[i]].checked ) {
total += a2[i]
}
}
document.NumberBuilder.result.value=total
}
</script>

</head>
<body>
<form name="NumberBuilder">

<h2>
Number Builder
</h2>

Sum up the numbers to the year the University of Miami was founded to see a photo of the UM campus.

<p>
<b>Need a hint?</b>
<ul>
<li>Try checking all the boxes</li>
</ul>

<menu>
<table>
<script>
function makeCheckbox ( i ) {
document.write('<input type=checkbox name="',
a1[i], '" onClick=compute()>' )
}

for ( i=0; i<a1.length; i++) {
document.write("<tr><td>")
makeCheckbox(i)
document.write("</td><td>", a2[i], "</td></tr>" )
}

document.write('<tr><td colspan=2>-------------------</td></tr>')
document.write('<tr><td><input type=text name="result" size=4 value=0>')
document.write('</td><td>Total</td></tr>')
</script>
</table>
</menu>
</form>

<img src="https://upload.wikimedia.org/wikipedia/commons/3/3e/University_of_Miami_Otto_G._Richter_Library.jpg" alt="UM campus" width=checkForWidth() height=checkForHeight()>
<script>
function checkForWidth(){
if (document.NumberBuilder.result.value == 10){
return "1280";
}else{
return "0";
}
}
function checkForHeight(){
if (document.NumberBuilder.result.value == 10){
return "960";
}else{
return "0";
}
}
</script>

</body>
</html>

编辑:我使用了“无显示”并调用了一个函数来将显示更改为“阻止”

<html>
<head><title>
Number Builder JavaScript
</title>
<style>
.hidden{
display: none;
}
</style>
<script>
a1 = new Array (
'field1', 'field2', 'field3', 'field4', 'field5', 'field6', 'field7', 'field8' )

a2 = new Array(
5, 10, 15, 100, 245, 300, 500, 750 )

function compute() {
total = 0
for ( i=0; i<a2.length; i++ ) {
if ( document.NumberBuilder[a1[i]].checked ) {
total += a2[i]
}
}
document.NumberBuilder.result.value=total
if (document.NumberBuilder.result.value == 1925){
showPicture();
}
}

function showPicture(){
document.getElementById("campus").style.display = "block";
}
</script>

</head>
<body>
<form name="NumberBuilder">

<h2>
Number Builder
</h2>

Sum up the numbers to the year the University of Miami was founded to see a photo of the UM campus.

<p>
<b>Need a hint?</b>
<ul>
<li>Try checking all the boxes</li>
</ul>

<menu>
<table>
<script>
function makeCheckbox ( i ) {
document.write('<input type=checkbox name="',
a1[i], '" onClick=compute()>' )
}

for ( i=0; i<a1.length; i++) {
document.write("<tr><td>")
makeCheckbox(i)
document.write("</td><td>", a2[i], "</td></tr>" )
}

document.write('<tr><td colspan=2>-------------------</td></tr>')
document.write('<tr><td><input type=text name="result" size=4 value=0>')
document.write('</td><td>Total</td></tr>')
</script>
</table>
</menu>
</form>
<div class="hidden" id="campus">
<img src="https://upload.wikimedia.org/wikipedia/commons/3/3e/University_of_Miami_Otto_G._Richter_Library.jpg" id="campus" alt="UM campus" width="1280" height="960">
</div>

</body>
</html>

最佳答案

你没有倾听变化。您只是定义了函数......但您需要一些东西来触发它们。

该事件监听器在网站加载后执行回调函数[makeCheckbox]:

document.addEventListener("DOMContentLoaded", function makeCheckbox ( i ) {
document.write('<input type=checkbox name="',
a1[i], '" onClick=compute()>' )


for ( i=0; i<a1.length; i++) {
document.write("<tr><td>")
makeCheckbox(i)
document.write("</td><td>", a2[i], "</td></tr>" )
}

document.write('<tr><td colspan=2>-------------------</td></tr>') ;
document.write('<tr><td><input type=text name="result" size=4 value=0>') ;
document.write('</td><td>Total</td></tr>');
});

这段代码中有很多错误需要修复,但重要的是触发创建内容的函数。

关于javascript - 我可以通过 Javascript 函数将图像添加到 HTML 正文吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32813564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com