gpt4 book ai didi

javascript - 我的代码 javascript 函数 onchange() 有问题

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

这个脚本有什么问题?

function swapImage() {
debugger

var image = document.getElementById("imageToSwap");
var dropd = document.getElementById("swapImg");
image.src = dropd.options[dropd.selectedIndex].value;


var model = document.getElementById("model");
var heading = document.getElementById("heading3");
var textGrey = document.getElementById("textGrey");
var textGrey2 = document.getElementById("textGrey2");

if (dropd.value == "http://placehold.it/150x150") {
model.innerHTML = "A4";
heading.innerHTML = "This text matches A4 model";
textGrey.innerHTML = "kjhkjh we ewf kjikjkj we";
textGrey2.innerHTML = "hf efjkj efe edeeeeejm dff";
return false;
} else if (dropd.value == "http://placehold.it/350x150") {
model.innerHTML = "A6";
heading.innerHTML = "This text matches A6 model";
textGrey.innerHTML = "xxx xxxxx xxxxx xxxx";
textGrey2.innerHTML = "yy yyyy yyyy yy";
return false;
} else if (dropd.value == "http://placehold.it/350x250") {
model.innerHTML = "A8";
heading.innerHTML = "This text matches the A8 model";
textGrey.innerHTML = "zzzz zzzzz";
textGrey2.innerHTML = "pppp ppp pp p p";
return false;
}
}
<select id="swapImg" name="model" class="modelSelect" onchange="swapImage()">
<option value="http://placehold.it/150x150">A4</option>
<option value="http://placehold.it/350x150" selected="selected">A6</option>
<option value="http://placehold.it/350x250">A8</option>
</select>
<br>
<br>

<div id="carbox">
<h2 id="model" class="model">A6</h2>
<img id="imageToSwap" src="http://placehold.it/350x150" width="544" height="203" style="margin-left:275px; margin-top:-82px" />

<div id="carbox-bottom">
<h3 id="heading3" class="heading3">Loren ipsum dolor sit ame</h3>
<p id="textGrey" class="textGrey">Coisteahi fwior he qvbsi </p>
<p id="textGrey2" class="textGrey2">Coisteahi fwior he qvbsi dolo</p>
</div>
</div>

http://jsfiddle.net/6xsro2cj/

最佳答案

你的代码没有问题。但在 jsfiddle 中,要使其在全局范围内工作,您应该使用指定加载类型=无包装,否则脚本将在 onLoad 或 DOM Ready 中加载,这使得它不可用于您的 onchange 调用。刚刚在 JS 窗口部分使用“不换行”选项更新了您的 fiddle 。

更新:

完整代码

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title></title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="robots" content="noindex, nofollow">
<meta name="googlebot" content="noindex, nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1">



<style id="compiled-css" type="text/css">
img {
width: 200px;
}
</style>



</head>
<body>
<select id="swapImg" name="model" class="modelSelect" onchange="swapImage()">
<option value="http://placehold.it/150x150">A4</option>
<option value="http://placehold.it/350x150" selected="selected">A6</option>
<option value="http://placehold.it/350x250">A8</option>
</select>
<br>
<br>

<div id="carbox">
<h2 id="model" class="model">A6</h2>
<img id="imageToSwap" src="http://placehold.it/350x150" width="544" height="203" style="margin-left:275px; margin-top:-82px" />

<div id="carbox-bottom">
<h3 id="heading3" class="heading3">Loren ipsum dolor sit ame</h3>
<p id="textGrey" class="textGrey">Coisteahi fwior he qvbsi dolo wetiuyy thuoi loren ipsum dolar </p>
<p id="textGrey2" class="textGrey2">Coisteahi fwior he qvbsi dolo</p>
</div>
</div>


<!-- TODO: JS script is be added here -->

<script type="text/javascript">



function swapImage() {
debugger
var image = document.getElementById("imageToSwap");
var dropd = document.getElementById("swapImg");
image.src = dropd.options[dropd.selectedIndex].value;


var model = document.getElementById("model");
var heading = document.getElementById("heading3");
var textGrey = document.getElementById("textGrey");
var textGrey2 = document.getElementById("textGrey2");

if (dropd.value == "http://placehold.it/150x150") {
model.innerHTML = "A4";
heading.innerHTML = "This text matches A4 model";
textGrey.innerHTML = "kjhkjh we ewf kjikjkj we";
textGrey2.innerHTML = "hf efjkj efe edeeeeejm dff";
return false;
} else if (dropd.value == "http://placehold.it/350x150") {
model.innerHTML = "A6";
heading.innerHTML = "This text matches A6 model";
textGrey.innerHTML = "xxx xxxxx xxxxx xxxx";
textGrey2.innerHTML = "yy yyyy yyyy yy";
return false;
} else if (dropd.value == "http://placehold.it/350x250") {
model.innerHTML = "A8";
heading.innerHTML = "This text matches the A8 model";
textGrey.innerHTML = "zzzz zzzzz";
textGrey2.innerHTML = "pppp ppp pp p p";
return false;
}
}



</script>
</body>
</html>

关于javascript - 我的代码 javascript 函数 onchange() 有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53782172/

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