简体   繁体   English

简单的JavaScript onclick不触发功能-通过jsfiddle和Firebug检查语法

[英]Simple javascript onclick not firing function - syntax checked via jsfiddle and firebug

I'm calling a utility called Highslide in a javascript function. 我正在javascript函数中调用名为Highslide的实用程序。 Embedding the whole thing in the link like this works fine: 像这样将整个内容嵌入链接中可以正常工作:

<a href="http://localhost/theopscompany-local/test-page-1/" onclick = "return hs.htmlExpand(this, { 
            objectType: 'ajax', width: 400, 
            headingEval: 'this.a.title', wrapperClassName: 'titlebar' }     )">highslide test link1</a>

However, placing said code in a function and calling it via onclick like below doesn't work. 但是,将上述代码放在函数中并通过如下所示的onclick调用它是行不通的。 I've put it through jsfiddle and Firebug for syntax and all looks fine, but it just doesn't fire the Highslide code for some reason. 我已经将其通过jsfiddle和Firebug进行了语法解析,并且看起来都不错,但是由于某种原因,它并没有触发Highslide代码。 The linked page is loaded normally instead. 链接页面将正常加载。

<a href="http://localhost/theopscompany-local/test-page-1/" onclick = "highslideview2();">highslide test link2</a>
<script type="text/javascript">
function highslideview2() {
    return hs.htmlExpand(this, { 
        objectType: 'ajax', 
        headingEval: 'this.a.title', wrapperClassName: 'titlebar' });}
</script>

I know this is likely a very simple thing, so I apologize if it's below the level of the usual question, but I'm stumped and can't see why this doesn't work. 我知道这可能是一件非常简单的事情,因此,如果它低于常见问题的水平,我深表歉意,但是我很困惑,无法理解为什么这不起作用。 The purpose is to a)clean up the HTML links and b) let me call different Highslide configurations for different types of links, so it's important enough to pursue. 目的是为了a)清理HTML链接,以及b)让我为不同类型的链接调用不同的Highslide配置,因此进行跟踪非常重要。

Thanks in advance for any help! 在此先感谢您的帮助!

The return value is now the result of the code executed within highslideview2() and not the function run when the onclick handler is fired. 现在,返回值是在highslideview2()中执行的代码的结果,而不是在触发onclick处理程序时运行的函数。 Adding the return back into the inline onclick attribute should fix it. 将返回值添加回内联onclick属性应该可以解决该问题。

<a href="http://localhost/theopscompany-local/test-page-1/" onclick = "return highslideview2(this)">highslide test link2</a>
<script type="text/javascript">
function highslideview2(e) {
    return hs.htmlExpand(e, { 
        objectType: 'ajax', 
        headingEval: 'e.a.title', wrapperClassName: 'titlebar' });}
</script>

Try this 尝试这个

<a href="http://localhost/theopscompany-local/test-page-1/" onclick = "highslideview2()">highslide test link2</a>
<script type="text/javascript">
function highslideview2() {
    return hs.htmlExpand(this, { 
        objectType: 'ajax', 
        headingEval: 'this.a.title', wrapperClassName: 'titlebar' });}
</script>

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

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