简体   繁体   English

无法使jsfiddle代码在jsfiddle之外工作

[英]Can't make jsfiddle code work outside of jsfiddle

I found this code on jsfiddle and it works fine, inside of fiddle. 我在jsfiddle上找到了这段代码,并且在fiddle内部运行良好。 But when I try to put it inside of my own HTML file I can't get it to work. 但是,当我尝试将其放入自己的HTML文件中时,我无法使其正常工作。 I am sure I left something out of the script tag, but I just don't know what. 我确定我在脚本标签中没有留下任何东西,但是我只是不知道什么。 Any input would be greatly appreciated. 任何投入将不胜感激。

Here is the fiddle: Working Fiddle 这是小提琴: 工作小提琴

PS The code is supposed to pull info from the URL and fill the form. PS该代码应从URL中提取信息并填写表格。

-Thanks -谢谢

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Parser</title>
</head>

<body>
<p>Your first name:
<br /><input name="name (awf_first)" id="name (awf_first)" /></p>

<p>Your last name:
<br /><input name="name (awf_last)" id="name (awf_last)" /></p>

<p>Your email:
<br /><input name="email" id="email" /></p>

<p>Your password:
<br /><input name="password" id="password" /></p>


<script>
var hashParams = window.location.hash.substr(1).split('&'); // substr(1) to 
remove the `?`
for(var i = 0; i < hashParams.length; i++){
var p = hashParams[i].split('=');
document.getElementById(p[0]).value = decodeURIComponent(p[1]);;
}
</script>
</body>

</html>

Your error means that document.getElementById(p[0]) is not finding the #name and #email input elements. 您的错误意味着document.getElementById(p[0])找不到#name#email输入元素。

This could be for two reasons: 这可能有两个原因:

  1. You don't have any elements with id s of: name and email . 您没有id的任何元素: nameemail

Check your elements to make sure that you have given them correct id s. 检查您的元素,以确保为他们提供了正确的id Your code shows elements with id s of: name (awf_first) and name (awf_last) , when the id s should be: name and email . 您的代码显示id的元素: name (awf_first)name (awf_last) ,而id应当为: nameemail

  1. You are attempting to find the element(s) prior to them being read into the DOM. 您试图在将元素读入DOM之前找到它们。

Make sure that the script comes AFTER the bulk of the HTML (just before the close of the body tag) as it is shown in the Fiddle, so that by the time you run that line, the element(s) will already be in the DOM. 确保script显示在小提琴中显示的大部分HTML之后(就在body标签关闭之前),以便在您运行该行时,元素已经位于DOM。

Also, be aware that in your question, the line // substr(1) to remove the '?' 另外,请注意,在您的问题中, // substr(1) to remove the '?' is split across two lines and is currently causing an error in your code. 分为两行,当前正在导致代码错误。 And, within that comment change the back-ticks surrounding the ? 并且,在该评论中,更改围绕?的反引号? with single quotes. 用单引号引起来。

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

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