简体   繁体   English

在javascript中从另一个函数调用一个函数

[英]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. 一些小错误。

  1. document.write clears off the rest of the page, so the script you have slotted in has gone. document.write清除了页面的其余部分,因此您插入的脚本已经消失。 You could try instead of using document.write, using document.getElementById("id").InnerHtml to a div / span. 您可以尝试使用document.getElementById(“id”)。InnerHtml到div / span,而不是使用document.write。 (im not a js expert - so you may want to google that.), instead, for the document.writing. (我不是一个js专家 - 所以你可能想谷歌那个。),而不是document.writing。
  2. 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.

相关问题 如何从另一个jsp函数调用一个jsp的javascript函数 - how to call javascript function of one jsp from another jsp function 从另一个Javascript文件中调用一个Javascript文件中的函数? - Call a function in one Javascript file from another Javascript file? javascript从另一个调用函数 - javascript call a function from another Javascript陌生性-可以从脚本的一部分调用函数,但不能从另一部分调用函数 - Javascript strangeness - Able to call a function from one part of the script, but not another 使用AngularJS在setTimeout中从另一个JavaScript文件中调用一个函数 - Call function in one JavaScript file from another, in a setTimeout, and using AngularJS 从另一个函数中调用一个 javascript 函数 - call a javascript function from within another function 如何在另一个JavaScript函数中调用一个JavaScript函数? - How can I call one JavaScript function in another JavaScript function? Safari中无法从另一个JavaScript文件的功能调用一个JavaScript文件的功能 - Unable to call function of one JavaScript file from function of another JavaScript file in Safari 从另一个javascript webresource调用javascript函数 - Call javascript function from another javascript webresource 从另一个 JavaScript 文件调用 JavaScript 函数 - Call JavaScript function from another JavaScript file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM