如何在小部件外部调用变量

How can I call a variable outside the widget

嗨,我有一个 html 小部件,其中包含 javascript 代码和一个名为 snapAuthor 的变量,但是当我调用该变量时,控制台告诉我它 没有找到它,这是工具代码:

<b:widget id='LinkList7' locked='false' title='Joker Template' type='LinkList' visible='true'>
<b:includable id='AUTH'><b:if cond='data:widget.instanceId == &quot;LinkList500&quot;'><b:tag name='script' type='text/javascript'><b:loop values='data:links' var='link'>AuthorsInfo[&#39;<data:link.name/>&#39;]=&#39;<data:link.target.jsEscaped/>&#39;;</b:loop></b:tag><b:else/>
<b:tag name='script' type='text/javascript'>
(function(){var snapAuthor=AuthorsInfo.filter(function(a){return a.name===&#39;<data:title/>&#39;})[0];


if(snapAuthor!==undefined){snapAuthor.provided=true;

<b:loop values='data:links' var='link'>
<b:if cond='data:link.name contains &quot;-ad&quot;'>
snapAuthor[&#39;<data:link.name/>&#39;]=&#39;<data:link.target.jsEscaped/>&#39;;
<b:else/>
<b:switch var='data:link.name'>
<b:case value='rank'/>snapAuthor.rank=&#39;<data:link.target.escaped/>&#39;;
<b:case value='about'/>snapAuthor.about=&#39;<data:link.target.escaped/>&#39;;
<b:default/>snapAuthor.links[&#39;<data:link.name/>&#39;]=&#39;<data:link.target/>&#39;;
</b:switch></b:if></b:loop>}})();</b:tag></b:if>
<b:tag name='script' type='text/javascript'>
console.log(snapAuthor);
</b:tag>
</b:includable>
</b:widget>

请注意,我想从页面的其他地方调用变量,但控制台告诉我它没有找到它...

在您的代码中使用 Immediately-Invoked Function Expression (function(){})() 可防止变量在其范围外可见

试试这个

<b:widget id='LinkList7' locked='false' title='Joker Template' type='LinkList' visible='true'>

    <b:includable id='AUTH'>
        <b:if cond='data:widget.instanceId == "LinkList500"'>
            <b:tag name='script' type='text/javascript'>
                <b:loop values='data:links' var='link'>AuthorsInfo['<data:link.name/>']='<data:link.target.jsEscaped/>';</b:loop>
            </b:tag>
        <b:else/>
            <b:tag name='script' type='text/javascript'>

                var snapAuthor=AuthorsInfo.filter(function(a){return a.name==='<data:title/>'})[0];
                if(snapAuthor!==undefined){snapAuthor.provided=true;

                    <b:loop values='data:links' var='link'>

                        <b:if cond='data:link.name contains "-ad"'>
                            snapAuthor['<data:link.name/>']='<data:link.target.jsEscaped/>';
                        <b:else/>
                            <b:switch var='data:link.name'>
                                <b:case value='rank'/>snapAuthor.rank='<data:link.target.escaped/>';
                                <b:case value='about'/>snapAuthor.about='<data:link.target.escaped/>';
                                <b:default/>snapAuthor.links['<data:link.name/>']='<data:link.target/>';
                            </b:switch>
                        </b:if>

                    </b:loop>   
                }

            </b:tag>
        </b:if>

            <b:tag name='script' type='text/javascript'>
            console.log(snapAuthor);
            </b:tag>
    </b:includable>

</b:widget>