Javascript文本框到变量
我已经在做一个小项目一两天了。代码如下Javascript文本框到变量,javascript,Javascript,我已经在做一个小项目一两天了。代码如下 <!DOCTYPE html> <html> <head> <script> function search() { document.getElementById("text1").value window.location.hash = "myVariable"; } </script> </head> </body> <form name="myfor
<!DOCTYPE html>
<html>
<head>
<script>
function search()
{
document.getElementById("text1").value
window.location.hash = "myVariable";
}
</script>
</head>
</body>
<form name="myform">
<input type="text" name="text1" value="">
<input type="button" value="Search" onclick="search()">
</form>
<div style="height: 4000px"></div>
<span id='yeah'>I have successfully jumped.</span>
<div style="height: 4000px"></div>
</body>
</html>
函数搜索()
{
document.getElementById(“text1”).value
window.location.hash=“myVariable”;
}
我跳得很成功。
现在,您可能想知道我用这段代码想要实现什么?嗯,我想在文本框中输入一个值,然后它将跳转到节(节是文本框中的值)。它有点像搜索引擎,但事实并非如此
例如,该部分是yeah。当用户在文本框中输入yeah时,应该将其跳转到yeah部分。相反,什么也没发生。尽管我在互联网上到处寻找,但我还没有找到一个能满足我需求的答案,因此我恳请你向我解释我的问题是什么,并可能为我的问题提供解决方案
我正在使用Mozilla Firefox web浏览器(如果需要这些信息)。尝试以下操作:
function search()
{
var elID= document.getElementById("text1").value;
var el = document.getElementById(elID);
el.scrollIntoView(true);
}
元素.scrollIntoView()
方法将元素滚动到视图中
Dalorzo应该可以工作,但是如果您计划添加的不仅仅是这个函数,jQuery可能是比原始javascript更好的选择 你想做什么
$("#button1").click(function() {
var t = $("#text1").val();
alert(t);
$('html, body').animate({
scrollTop: $("#"+t).offset().top
}, 2000);
});
我建议采用另一种方法——对于像这样的简单任务,原始JavaScript是一种更受欢迎的解决方案,它会降低开销。没错,它要重得多。如果这是唯一需要的javascript,那么是的,raw是合乎逻辑的。我假设这个函数将被扩展,这就是我想用这个函数做的。然而,jQuery看起来很有趣,我想我会读更多的内容。所以我想你确实在某种程度上解决了我的问题。