在 onclick上调用来自全局函数的javascript变量参数

时间:2021-06-20 11:03:44

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. The event works fine if the parameters are static. I want to get a the value from the global function. The example is as follows:

我现在绝望地找到解决这个问题的方法。我试图在onclick上调用一个函数,甚至在另一个函数内。如果参数是静态的,则事件可以正常工作。我想从全局函数中获取值。示例如下:

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? Because I get an undentified error message.

如何识别xmldoc和newsresult?因为我收到一个未识别的错误消息。

I will be greatfull if you help me.

如果你帮助我,我会很高兴。

2 个解决方案

#1


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. 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处理程序创建闭包。

#2


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()并重构事物的范围可能会使代码更清晰,更易于维护。

if you are mixing HTML and Javascript, you are usually on the wrong track.

如果你混合HTML和Javascript,你通常是在错误的轨道上。

#1


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. 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处理程序创建闭包。

#2


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()并重构事物的范围可能会使代码更清晰,更易于维护。

if you are mixing HTML and Javascript, you are usually on the wrong track.

如果你混合HTML和Javascript,你通常是在错误的轨道上。