如何在Sublime Text 3中将此代码用作JS的Snippet?

时间:2017-12-22 17:51:16

标签: javascript jquery sublimetext3 code-snippets

当我尝试使用时,美元符号阻止代码段正常工作......

$('.modal').on('hide.bs.modal', function(e) {    
    var $if = $(e.delegateTarget).find('iframe');
    var src = $if.attr("src");
    $if.attr("src", '/empty.html');
    $if.attr("src", src);
});

1 个答案:

答案 0 :(得分:1)

您需要使用Back Slash($)转义美元符号(\),同时创建带有sublime文本的新代码段。您的代码段内容应为:<content>

<snippet>
    <content><![CDATA[
\$('.modal').on('hide.bs.modal', function(e) {    
    var \$if = \$(e.delegateTarget).find('iframe');
    var src = \$if.attr("src");
    \$if.attr("src", '/empty.html');
    \$if.attr("src", src);
});
]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <tabTrigger>myscript</tabTrigger>
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <!-- <scope>source.python</scope> -->
</snippet>