gpt4 book ai didi

javascript - 显示选定的图像、网页

转载 作者:行者123 更新时间:2023-11-30 09:13:09 27 4
gpt4 key购买 nike

我正在尝试显示用户从列表中选择的图像,但我在屏幕上看不到任何内容。

<!DOCTYPE html>
<html>
<body>
<style>
.container {
position: relative;
}

.center {
position: absolute;
left: 0;
top: 50%;
width: 100%;
text-align: center;
font-size: 18px;
}

img {
width: 100%;
height: auto;
opacity: 1;
}
</style>

<p>Images:</p>
<select id="ImSelect" size="6" onchange "getvalue()">
<option value="LB_1_000561">LB_1_000561</option>
<option value="LB_1_000562">LB_1_000562</option>
<option value="LB_1_000563">LB_1_000563</option>
<option value="LB_1_000564">LB_1_000564</option>
<option value="LB_1_000565">LB_1_000565</option>
<option value="LB_1_000566">LB_1_000566</option>
<option value="LB_1_000567">LB_1_000567</option>
<option value="LB_1_000568">LB_1_000568</option>
<option value="LB_1_000569">LB_1_000569</option>
</select>

<div class="container" id="x"></div>

<script>
function getvalue() {
var value_ = document.getElementById("ImSelect").value;
img.src = "Images/" + value_ + ".jpg";
var img = document.createElement("img");
var src = document.getElementById("x");
src.appendChild(img);
}
</script>
</body>
</html>

最佳答案

那是因为没有任何东西会调用您的 getValue 函数。您在 onchange 之后需要一个 =,如 onchange="getvalue()"But, you shouldn't be setting up event handlers in HTML in the first place.每当您在 select 中进行选择时,您都需要调用该函数,因此需要为其设置一个 change 事件处理程序,并且该处理程序应设置在JavaScript。

此外,您正在尝试在创建图像之前设置图像的 src。您需要交换这两行。

此外,您的文档没有 head 部分,您的 style 元素应该放在那里,而不是 body .

最后,您的代码将创建一个新图像,并在每次进行选择时将其附加到文档中,因此您最终可能会显示许多图像。如果您只想显示一张图片(最近选择的一张),那么无需创建新图片并将其附加到文档中,只需在页面中您想要的位置创建一个静态 img 元素,但不要在 HTML 中设置它的 src。然后,您的 JavaScript 需要做的就是访问该预先存在的图像并设置其 src 而不是生成新图像并附加它。

<!DOCTYPE html>
<html>
<head>
<style>
.container {
position: relative;
}

.center {
position: absolute;
left: 0;
top: 50%;
width: 100%;
text-align: center;
font-size: 18px;
}

img {
width: 100%;
height: auto;
opacity: 1;
}
</style>
</head>
<body>
<p>Images:</p>
<select id="ImSelect" size="6">
<option value="LB_1_000561">LB_1_000561</option>
<option value="LB_1_000562">LB_1_000562</option>
<option value="LB_1_000563">LB_1_000563</option>
<option value="LB_1_000564">LB_1_000564</option>
<option value="LB_1_000565">LB_1_000565</option>
<option value="LB_1_000566">LB_1_000566</option>
<option value="LB_1_000567">LB_1_000567</option>
<option value="LB_1_000568">LB_1_000568</option>
<option value="LB_1_000569">LB_1_000569</option>
</select>

<div class="container">
<!-- Each new image will display in this one elmeent -->
<img id="target" src="" alt="">
</div>

<script>
let select = document.getElementById("ImSelect");
let target = document.getElementById("target");

// Set up the change event hanlder in JavaScript, not in HTML
select.addEventListener("change", getvalue);

function getvalue() {
// Just update the src of the pre-existing img element
target.src = "Images/" + select.value + ".jpg";
target.alt = target.src; // Just to show something on the screen for demo
}
</script>
</body>
</html>

关于javascript - 显示选定的图像、网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57099121/

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