猿问

Javascript将元素Id作为参数传递

我需要一个 html 页面中的一堆滑块。我发现w3schools 中的这个例子正是我所需要的。


在那里,以下脚本处理恰好一个硬编码滑块的更新:


<body>


<h1>Custom Range Slider</h1>

<p>Drag the slider to display the current value.</p>


<div class="slidecontainer">

  <input type="range" min="1" max="100" value="50" class="slider" id="myRange">

  <p>Value: <span id="demo"></span></p>

</div>


<script>

var slider = document.getElementById("myRange");

var output = document.getElementById("demo");

output.innerHTML = slider.value;


slider.oninput = function() {

  output.innerHTML = this.value;

}

</script>


</body>

问题是如果我向 HTML 页面添加更多滑块,我该如何概括该脚本?我不想为我在页面中需要的每个滑块都有一对滑块和输出和 oninput 函数。


猛跑小猪
浏览 271回答 3
3回答

慕莱坞森

这个怎么样:var elements = document.getElementsByClassName("slidecontainer");elements.forEach(function(el){&nbsp; var slider = el.getElementById("myRange");&nbsp; var output = el.getElementById("demo");&nbsp; output.innerHTML = slider.value;&nbsp; slider.oninput = function() {&nbsp; &nbsp; output.innerHTML = this.value;&nbsp; }})现在,对于您添加的每个滑块,它都会起作用,这意味着您只需要添加具有相应类的 html</head><body><h1>Custom Range Slider</h1><p>Drag the slider to display the current value.</p><div class="slidecontainer">&nbsp; <input type="range" min="1" max="100" value="50" class="slider" id="myRange">&nbsp; <p>Value: <span id="demo"></span></p></div><div class="slidecontainer">&nbsp; <input type="range" min="1" max="100" value="50" class="slider" id="myRange">&nbsp; <p>Value: <span id="demo"></span></p></div>将产生两个没有额外声明的工作滑块。所以我正在做的是寻找每个具有滑块容器类的元素并遍历每个元素。这样对于具有该结构的每个滑块,您都会有一个滑块。无需添加额外的 javascript 来引用每个滑块

饮歌长啸

将滑块和输出标签作为参数来运行function setSlider(slider, output){&nbsp; var slider = document.getElementById(slider);&nbsp; var output = document.getElementById(output);&nbsp; output.innerHTML = slider.value;&nbsp; slider.oninput = function() {&nbsp; &nbsp; output.innerHTML = this.value;&nbsp; }}setSlider("myRange1", "demo1")setSlider("myRange2", "demo2")<body><h1>Custom Range Slider</h1><p>Drag the slider to display the current value.</p><div class="slidecontainer">&nbsp; <input type="range" min="1" max="100" value="50" class="slider" id="myRange1">&nbsp; <p>Value: <span id="demo1"></span></p></div><div class="slidecontainer">&nbsp; <input type="range" min="1" max="100" value="50" class="slider" id="myRange2">&nbsp; <p>Value: <span id="demo2"></span></p></div></body>

MMMHUHU

您可以使用类名并定位容器元素,如下所示:const containers = document.querySelectorAll(".slidecontainer");for(let container of containers) {&nbsp; const slider = container.querySelector(".slider");&nbsp; const output = container.querySelector(".value");&nbsp; output.innerHTML = slider.value;&nbsp; slider.oninput = function() {&nbsp; &nbsp; output.innerHTML = this.value;&nbsp; }}<body><h1>Custom Range Slider</h1><p>Drag the slider to display the current value.</p><div class="slidecontainer">&nbsp; <input type="range" min="1" max="100" value="50" class="slider">&nbsp; <p>Value: <span class="value"></span></p></div><div class="slidecontainer">&nbsp; <input type="range" min="1" max="100" value="50" class="slider">&nbsp; <p>Value: <span class="value"></span></p></div><div class="slidecontainer">&nbsp; <input type="range" min="1" max="100" value="50" class="slider">&nbsp; <p>Value: <span class="value"></span></p></div>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答