Javascript文本框到变量(Javascript Textbox to Variable)

编程入门 行业动态 更新时间:2024-10-26 11:13:27
Javascript文本框到变量(Javascript Textbox to Variable)

我一直在做一个小项目一两天。 代码如下。

<!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>

现在您可能想知道我想用这段代码完成什么? 好吧,我想在文本框中输入一个值,然后它会跳转到该部分(该部分是文本框中的值)。 它有点像搜索引擎,但事实并非如此。

例如,该部分是的。 当用户在文本框中输入是时,应该将它们跳到是的部分。 相反没有任何反应 尽管在互联网上看到了我没有找到满足我需求的答案,所以我请你帮我解释一下我的问题是什么,并可能给我一个解决我问题的方法。

我正在使用Mozilla Firefox Web浏览器(如果需要该信息)。

I have been working on a little project for a day or two. The code is the following.

<!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>

Now you may be wondering what am I trying to accomplish with this code? Well, I want to be to enter a value in the text box and then it will jump me to the section (the section is the value in the text box). It is sort of like a search engine, but it is not.

For example the section is yeah. When a user enters yeah in the text box it is supposed to jump them to the yeah section. Instead nothing happens. And despite looking all over the Internet I have not found an answer that satisfies my needs, so I would kindly ask that you please explain to me what my problem is and possibly give me a solution to my problem.

I am using the Mozilla Firefox web browser (if that information is necessary).

最满意答案

Dalorzo应该可以工作,但是如果你计划添加的不仅仅是这个函数,jQuery可能是比原始javascript更好的选择。

这是你想要做的事情的小提琴 。

$("#button1").click(function() { var t = $("#text1").val(); alert(t); $('html, body').animate({ scrollTop: $("#"+t).offset().top }, 2000); });

Dalorzo's should work, but jQuery could be the better option than raw javascript if you plan to add more than just this function.

Here's a fiddle of what you're trying to do.

$("#button1").click(function() { var t = $("#text1").val(); alert(t); $('html, body').animate({ scrollTop: $("#"+t).offset().top }, 2000); });

更多推荐

text,value,电脑培训,计算机培训,IT培训"/> <meta name="description"

本文发布于:2023-08-07 23:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466027.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   文本框   Javascript   Textbox   Variable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!