[英]Call one function from another function in javascript
I'm still a novice with web technologies and I have a few questions in line with the following code. 我仍然是网络技术的新手,我有几个问题符合以下代码。
I'm trying to call a function getDetails() from another javascript function displayTable(). 我正在尝试从另一个javascript函数displayTable()调用函数getDetails()。 displayTable() gets invoked on clicking the button 'My CD Facts'.
单击“我的CD事实”按钮时会调用displayTable()。
Well, its not working for me. 好吧,它不适合我。 I guess its some thing daft but I'm not able to figure out.
我猜它有点蠢,但我无法搞清楚。 I tried to diagnose it with firebug and it says getDetails() is not defined.
我尝试用firebug进行诊断,并说没有定义getDetails()。
Also, I have a basic css file for displaying the table in a particular style. 另外,我有一个基本的css文件,用于以特定样式显示表。 That's not working either.
这也不起作用。 Is it because I linked it in the body and I'm using it in the head?
是因为我将它与身体相连并且我在头部使用它?
<script type="text/javascript">
var xmlDoc;
function displayTable()
{
var artistName;
if (window.XMLHttpRequest)
{
xmlDoc=new window.XMLHttpRequest();
xmlDoc.open("GET","Artists.xml",false);
xmlDoc.send("");
xmlDoc=xmlDoc.responseXML;
}
else if (ActiveXObject("Microsoft.XMLDOM"))
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("Artists.xml");
}
document.write("<table class=\"artistTable\" border='1'>");
document.write("<th>Artist</th> <th>Title</th>");
var x=xmlDoc.getElementsByTagName("CD");
for (i=0;i<x.length;i++)
{
artistName = x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue;
document.write("<tr><td><a href=\"javascript:getDetails(artistName );\">");
document.write(artistName);
document.write("</a></td></tr>");
}
document.write("</table>");
}
function getDetails(artistName )
{
alert(artistName);
}
</script>
</head>
<body>
<link rel="stylesheet" type="text/css" href="style.css">
<form>
<input type="button" value="My CD Facts" onclick="displayTable()"/>
</form>
</body>
</html>
cheers 干杯
getDetails() is not a Javascript buit-in function. getDetails()不是Javascript buit-in函数。 It is a user defined function.
它是用户定义的功能。 The people that wrote the book Head First Ajax defined their version of the getDetails() on page 24.
编写Head First Ajax一书的人定义了第24页的getDetails()版本。
document.write("<tr><td><a href=\"javascript:getDetails(" + artistName + ");\">");
A couple of minor mistakes. 一些小错误。
what rony said, but with a couple of single inverted commas. 罗尼说,但有一些单引号。
document.write("< tr>< td>< a href=\\"javascript:getDetails('" + artist + "');\\">"); document.write(“<tr> <td> <a href = \\”javascript:getDetails('“+ artist +”'); \\“>”);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.