简体   繁体   English

在 onclick 上从全局 function 调用<a href>javascript</a>变量参数

[英]Calling javascript variable parameters from global function on the <a href > onclick even

I am desparate now to find a solution to this problem.我现在很想找到解决这个问题的方法。 I'm trying to call a function on the onclick even inside another function.我正在尝试在 onclick 上调用 function,即使在另一个 function 内也是如此。 The event works fine if the parameters are static.如果参数为 static,则该事件可以正常工作。 I want to get a the value from the global function.我想从全局 function 中获取值。 The example is as follows:示例如下:

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);

for (var p=1; p <= pages;p++){
    pagesref = pagesref+"<a  href='#' onclick='newsdatainfo(xmldoc,newsresult,6,10);' >"+p+"</a> | ";
}

How do I get the xmldoc and newsresult to be identified?如何识别 xmldoc 和 newsresult? Because I get an undentified error message.因为我收到一条不明错误消息。

I will be greatfull if you help me.如果你帮助我,我会很高兴的。

Did you try "document.xmldoc"?你试过“document.xmldoc”吗?

What scopes are the variable definitions in?变量定义在哪些范围内? If they are private (within a function), they will not be visible from outside that function.如果它们是私有的(在函数内),它们将不会从 function 外部可见。 In this case, you would need to make them either global (put them into the document's scope), or create closures for your onclick handlers.在这种情况下,您需要将它们设为全局(将它们放入文档的范围),或者为您的 onclick 处理程序创建闭包。

You could also try observing the click event (choose favorite framework here).您也可以尝试观察点击事件(在此处选择最喜欢的框架)。

Prototype: $(ele).observe('click').newsdatainfo() and refactoring the scope of things might make the code cleaner and easier to maintain.原型: $(ele).observe('click').newsdatainfo()并重构 scope 的东西可能会使代码更清洁和更易于维护。

if you are mixing HTML and Javascript, you are usually on the wrong track.如果您将 HTML 和 Javascript 混合在一起,您通常会走错路。

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

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