简体   繁体   English

JavaScript新手-为什么我的函数无法执行?

[英]New to JavaScript - why won't my function execute?

I'm taking an intro JavaScript course and I'm following all of the directions but the function is not executing. 我正在学习JavaScript入门课程,并且遵循所有说明,但是该函数未执行。 I have viewed many "JavaScript won't execute" questions on StackOverflow and none of them contained an answer that would solve my problem. 我已经在StackOverflow上查看了许多“ JavaScript无法执行”问题,但没有一个问题可以解决我的问题。

Here is the HTML header: 这是HTML标头:

<!DOCTYPE>
<html>
<head>
<title>Monroe Public Library</title>
<link href="mplstyles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="random.js"></script>
<script>
/*
The showImg() function displays a random image from the 0.jpg through 9.jpg files. The
random image is designed to thwart hackers attempting to enter the library records data
base by requiring visual confirmation
*/

function showImg() {
    var imgNumber = randomInteger(9);
    // Return a random number from 0 to 9
    document.write("<img 'src=" + imgNumber + ".jpg' alt='' />");
}

</script>
</head>

Here is the portion of my HTML calling the function. 这是我的HTML调用函数的部分。 It's supposed to call four times in order to output the HTML string five times and show five of the number images on the webpage. 为了将HTML字符串输出五次并在网页上显示五个数字图像,应该调用四次。

<table border="1" cellpadding="5" cellspacing="0">
<tr>
   <th>Username</th>
   <td><input size="20" /></td>
</tr>
<tr>    
   <th>Password</th>
   <td><input type="password" size="20" /></td>
</tr>
<tr>
   <td>As a final security check, enter the 5 numbers 
   you see displayed below.</td>
   <td><input size="6" /></td>
</tr>
<tr>
   <td colspan="2" class="center">
   <input type="button" value="View Library Records" />
   </td>
</tr>
<tr>
   <td colspan="2" class="center">
   <script>showImg();showImg();showImg();showImg();showImg();</script>
   </td>
</tr>
</table>
</div>

When I open the page it doesn't output anything; 当我打开页面时,它什么也不会输出。 just stays the way I wrote it with the script element and function call. 只是保持了我通过script元素和函数调用编写它的方式。 The table cell simply shows up as empty. 表格单元格只是显示为空。 I've put a message box into the function to see if it even enters it at all, and it does, but the message box only appears once (no matter how many times I call the function). 我已经在函数中放置了一个消息框,以查看它是否完全输入了,并且确实输入了,但是消息框仅出现一次(无论我调用该函数多少次)。

The JavaScript file (random.js) as well as the HTML is all provided and working so I only included the relevant sections. JavaScript文件(random.js)以及HTML均已提供并且可以正常工作,因此我仅包括相关部分。

Not sure if this is related, but you have "<img 'src=" instead of "<img src='" 不知道这是否相关,但是您有"<img 'src="而不是"<img src='"

Other than that, defining 除此之外,定义

function randomInteger(rng) {
    return Math.round(Math.random() * rng);
}

and changing your document.write line to 并将您的document.write行更改为

document.write("<b>" + imgNumber + ".jpg</b> ");

worked fine for me. 对我来说很好。

This line 这条线

 document.write("<img 'src=" + imgNumber + ".jpg' alt='' />");

Has an apostrophe out of place. 撇号不正确。 It should be 它应该是

 document.write("<img src='" + imgNumber + ".jpg' alt='' />");

 <table border="1" cellpadding="5" cellspacing="0"> <tr> <th>Username</th> <td><input size="20" /></td> </tr> <tr> <th>Password</th> <td><input type="password" size="20" /></td> </tr> <tr> <td>As a final security check, enter the 5 numbers you see displayed below.</td> <td><input size="6" /></td> </tr> <tr> <td colspan="2" class="center"> <input type="button" value="View Library Records" /> </td> </tr> <tr> <td colspan="2" class="center"> <script> function showImg() { var imgNumber = Math.floor(Math.random() * 9); document.write("<img src='" + imgNumber + ".jpg' alt='' />"); } showImg();showImg();showImg();showImg();showImg(); </script> </td> </tr> </table> 

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

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