简体   繁体   English

在 href 中使用 javascript 变量出错了 - 为什么?

[英]use javascript variable in href went wrong - why?

there are many Questions and Answers out there for how to get a Javascript Variable into the href tag, but I am running into a Problem which no one seems to have.有很多关于如何将 Javascript 变量放入 href 标签的问题和答案,但我遇到了一个似乎没有人遇到的问题。

I have a slider and depending on what position the slider is set, the value should be taken into the href.我有一个滑块,根据滑块设置的位置,该值应包含在 href 中。

So if the sliders value is 50 the link should be "http//testurl.de/50" However I get always back " http://testurl.de/[object%20HTMLSpanElement] " And don't know what's wrong.因此,如果滑块值为 50,则链接应为“http//testurl.de/50”但是我总是返回“ http://testurl.de/[object%20HTMLSpanElement] ”并且不知道出了什么问题。

<div class="slidecontainer">
<input type="range" min="1" max="100" value="50" class="slider" id="ValueSlider">
  <p>Value: <span id="SetValue"></span></p>
</div>



<script>
  var slider = document.getElementById("ValueSlider");
  var output = document.getElementById("SetValue");
  output.innerHTML = slider.value;

  slider.oninput = function() {
  output.innerHTML = this.value;
  }

</script>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​



<a href="#" onclick="window.location='http://testurl.de/' + SetValue">link</a>

Any help would be much appreciated!任何帮助将非常感激! :) Best Mo :) 最好的莫

You have two ways to fix this, and you actually do them both already a few lines above that code.你有两种方法来解决这个问题,实际上你已经在代码上面几行了。

  • SetValue is the ID of your span element, so to get the data from that element, you can use SetValue.innerHTML , SetValue.innerText or a few others. SetValue是您的span元素的 ID,因此要从该元素获取数据,您可以使用SetValue.innerHTMLSetValue.innerText或其他一些。
  • Or, you can just reference the input element you have there (looks like you call it slider ) and grab slider.value again.或者,您可以只引用您在那里拥有的输入元素(看起来您称它为slider )并再次抓取slider.value

<a href="#" onclick="window.location='http://testurl.de/' + SetValue.innerHTML">link</a>
// or
<a href="#" onclick="window.location='http://testurl.de/' + SetValue.innerText">link</a>
// or
<a href="#" onclick="window.location='http://testurl.de/' + SetValue.textContent">link</a>
// or
<a href="#" onclick="window.location='http://testurl.de/' + slider.value">link</a>

You should use output.innerHTML and not SetValue on the link, as bellow:您应该在链接上使用output.innerHTML而不是SetValue ,如下所示:

<a href="#"
   onclick="window.location='http://testurl.de/' + output.innerHTML">link
</a>

simply this就是这个

 const slider = document.getElementById("ValueSlider") , output = document.getElementById("SetValue") , aLink = document.getElementById("SliderLink") ; function SetSliderVal() { output.textContent = slider.value aLink.href = 'http://testurl.de/'+slider.value aLink.textContent = 'link = '+slider.value } SetSliderVal(); slider.oninput = SetSliderVal
 <div class="slidecontainer"> <input type="range" min="1" max="100" value="50" class="slider" id="ValueSlider"> <p>Value: <span id="SetValue"></span></p> </div> <a href="#" id="SliderLink">link</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM