简体   繁体   English

如何从HTML的for循环中调用jQuery函数

[英]How to call a jQuery function from a for-loop in HTML

so this may be a unique question 所以这可能是一个独特的问题

I have this XSL for-each loop which generates 3 strings Music, TV, Comedy 我有这个XSL for-each循环,它生成3个字符串音乐,电视,喜剧

The HTML (and my javascript attempt) Uncaught SyntaxError HTML(和我的JavaScript尝试) 未捕获的SyntaxError

<div id="rgt" style="position:absolute; height:1px; visibility:hidden;">
    <xsl:for-each select="$GenreTypes/item">
        <div id="rgt-{position()}">
            <xsl:value-of select="sc:fld('title',.)" /><!-- Spits out Music, TV, Comedy -->

            <!-- Where I'm trying to call my function to add titles to an Array -->
            <script type="text/javascript">$.addRelatedGenres('{sc:fld('title',.)}');</script>
         </div>
     </xsl:for-each>
</div>

Basically I'm needing to capture these values into an Array in jQuery. 基本上,我需要将这些值捕获到jQuery中的Array中。 I am 'printing' out the strings I need to a hidden div, as each string is being 'printed' by the for-each loop, I want to call my jQuery function and add each string to the Array. 我正在“打印”我需要的字符串到一个隐藏的div,因为每个字符串都被for-each循环“打印”,所以我想调用我的jQuery函数并将每个字符串添加到Array中。

My jQuery function 我的jQuery函数

function addRelatedGenres(rgt_name) {
    console.log(rgt_name);
}

So far I'm getting syntax errors, not sure how to call a Javascript / jQuery function from the page that isn't an onClick or dependant on the page url (everything happens on 1 page, so there is no new pages, just divs that get unhidden and hidden). 到目前为止,我遇到语法错误,不确定如何从不是onClick或不依赖页面URL的页面调用Javascript / jQuery函数(一切都发生在1个页面上,因此没有新页面,只有divs被隐藏和隐藏)。

Perhaps there's a better way to do this? 也许有更好的方法可以做到这一点?

Thanks for taking a peek! 感谢您浏览! :) :)

Your function is not a "jQuery function" - it's javascript. 您的函数不是“ jQuery函数”,而是javascript。 You need to remove the $. 您需要删除$. from before the call. 从通话之前开始。

Shouldn't: 不应该:

<script type="text/javascript">$.addRelatedGenres('{sc:fld('title',.)}');</script>

be

<script type="text/javascript">$.addRelatedGenres('<xsl:value-of select="sc:fld('title',.)" />');</script>

?

IIRR, the { } syntax only works in attributes. IIRR,{}语法仅适用于属性。

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

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