Changing “src” attribute of [removed]

2020-01-29 01:55发布

Is it possible to change the "src" attribute of an existing <script> element using Jquery.attr()? It seemed like a simple way to get JSONP to work but I am not able to make this work for me.

3条回答
Lonely孤独者°
2楼-- · 2020-01-29 02:14

Whilst, yes, this is possible, you don't need to do this manually as jQuery has good built-in support for JSON-P requests. See the documentation for the $.getJSON and $.ajax methods.

查看更多
不美不萌又怎样
3楼-- · 2020-01-29 02:27

It turns out that a script's src can only be set once! It is not possible to change the src attribute of an existing <script> element in the DOM. However, a dynamically created <script> element can have its source set (but exactly once!)

查看更多
姐就是有狂的资本
4楼-- · 2020-01-29 02:34

If you're trying to load a script, you could instead use getScript.

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

Or, if you want to change the src for another reason, since script tags can't have id's, you could either use an HTML5 compliant data-id attribute, or match using the existing src. Then you could just change the src value using attr. But this may not load the script on all browsers.

查看更多
登录 后发表回答