在 onclick事件上从全局函数调用javascript变量参数

时间:2022-09-13 00:38:03

The I wrote in the <a href='javascript:newsdatainfo(document.xmldoc,...see below. But it still does not work.

我在

var dataxml = unescape(xml);    
var xmldoc = getDomAdapter().parseXml(dataxml);
var rootnode = xmldoc.getElementsByTagName('result_set')[0];
var newsresult = rootnode.getElementsByTagName('item');
var pages = Math.round(newsresult.length / 10);

var pagesref;
for (var p=1; p <= pages;p++){
    if (p==1)
        pagesref = pagesref+"<a href='javascript:newsdatainfo(document.xmldoc,document.newsresult,6,10)' >"+p+"</a> | ";

1 个解决方案

#1


Echoing other responses I don't know quite what you're asking here. In particular never say "it doesn't work - state what actions you performed, what your expected output was and what your actual output was. Though anyway:

回应其他回复我不知道你在这里问什么。特别是永远不要说“它不起作用 - 陈述你执行了什么动作,你的预期输出是什么以及你的实际输出是什么。尽管如此:

  1. You shouldn't use a javascript: prefix in a href; you should actually use the onclick event that you referenced in your question title.
  2. 你不应该在href中使用javascript:前缀;您应该实际使用您在问题标题中引用的onclick事件。

  3. You can just use xmldoc and rootnode unprefixed.
  4. 您可以使用xmldoc和rootnode取消固定。

  5. If you don't want the hyperlink to actually go anywhere, make sure you return false from the onclick handler to prevent the click from being handled. Also set the URL to "#" (which will typically point to the top of the current page), so that the link will degrade gracefully with Javascript disabled and so that the address in the status bar on hover makes sense (unless you want to override that too).
  6. 如果您不希望超链接实际到达任何位置,请确保从onclick处理程序返回false以防止单击被处理。同时将URL设置为“#”(通常指向当前页面的顶部),以便在禁用Javascript时优雅地降低链接,以便悬停状态栏中的地址有意义(除非您想要也覆盖它)。

  7. I hope that code ends abruptly, since looping over all elements just to perform some action on the first one is suboptimal, to say the least.
  8. 我希望代码突然结束,因为至少可以说,遍历所有元素只是为了对第一个执行某些操作是次优的。

Thus I believe your last line could be written as

因此,我相信你的最后一行可以写成

pagesref = pagesref+"<a href='#' onclick='newsdatainfo(xmldoc,newsresult,6,10); return false' >"+p+"</a> | ";

and things might work as you expect, however that may be.

并且事情可能会按照您的预期发挥作用,但可能会如此。

#1


Echoing other responses I don't know quite what you're asking here. In particular never say "it doesn't work - state what actions you performed, what your expected output was and what your actual output was. Though anyway:

回应其他回复我不知道你在这里问什么。特别是永远不要说“它不起作用 - 陈述你执行了什么动作,你的预期输出是什么以及你的实际输出是什么。尽管如此:

  1. You shouldn't use a javascript: prefix in a href; you should actually use the onclick event that you referenced in your question title.
  2. 你不应该在href中使用javascript:前缀;您应该实际使用您在问题标题中引用的onclick事件。

  3. You can just use xmldoc and rootnode unprefixed.
  4. 您可以使用xmldoc和rootnode取消固定。

  5. If you don't want the hyperlink to actually go anywhere, make sure you return false from the onclick handler to prevent the click from being handled. Also set the URL to "#" (which will typically point to the top of the current page), so that the link will degrade gracefully with Javascript disabled and so that the address in the status bar on hover makes sense (unless you want to override that too).
  6. 如果您不希望超链接实际到达任何位置,请确保从onclick处理程序返回false以防止单击被处理。同时将URL设置为“#”(通常指向当前页面的顶部),以便在禁用Javascript时优雅地降低链接,以便悬停状态栏中的地址有意义(除非您想要也覆盖它)。

  7. I hope that code ends abruptly, since looping over all elements just to perform some action on the first one is suboptimal, to say the least.
  8. 我希望代码突然结束,因为至少可以说,遍历所有元素只是为了对第一个执行某些操作是次优的。

Thus I believe your last line could be written as

因此,我相信你的最后一行可以写成

pagesref = pagesref+"<a href='#' onclick='newsdatainfo(xmldoc,newsresult,6,10); return false' >"+p+"</a> | ";

and things might work as you expect, however that may be.

并且事情可能会按照您的预期发挥作用,但可能会如此。