简体   繁体   English

jQuery函数中的脚本src

[英]Script src inside jquery function

I want to do something like this: 我想做这样的事情:

            $("#startButton").click(function() {
                <?php if(somecondition){ ?>
                    <script src="some-external-js-srouce"></script>
                    <noscript><img src="irrelevant"></noscript>
<?php } ?>

});

Meaning that I want to execute some external javascript inside a jquery function if a button has been clicked. 这意味着如果单击按钮,我想在jquery函数内执行一些外部javascript。 Can you help me? 你能帮助我吗?

You're putting HTML inside JavaScript. 您正在将HTML放入JavaScript。 That's not quite possible. 那是不可能的。

You probably want $.getScript to load and execute a script dynamically at runtime. 您可能希望$.getScript在运行时动态加载并执行脚本。 It does not support <noscript> but you're using JavaScript for it anyway, so that does not really make sense to me... 它不支持<noscript>但是无论如何您都在使用JavaScript,所以这对我来说真的没有意义...

if you want to execute a javascript function from an external file then you can try like this - 如果您想从外部文件执行javascript函数,则可以尝试这样-

<script type="text/javascript" src="myjs.js">
// code that will refer functions of myjs.js

</script>

use jQuery.getScript( url, function(){}); 使用jQuery.getScript(url,function(){});

http://api.jquery.com/jQuery.getScript/ http://api.jquery.com/jQuery.getScript/

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

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