如何在不重新加载页面的情况下更改src

时间:2022-08-26 11:28:34

i was looking for answer on * but didn't found it. i'm creating a color switcher

我正在寻找*的答案,但没有找到它。我正在创建一个颜色切换器

如何在不重新加载页面的情况下更改src

i have a default path <script src="js/theme/theme-default.js"></script> which is working fine on my website

我有一个默认路径

i also created color switcher code

我还创建了颜色切换器代码

My HTML

 <a href="#" data-style="theme-blue" title="blue: #3498DB" style="background: #3498DB">
        </a>

My JS

 $('.color-changer a').on('click', function(){
        var style = $(this).attr('data-style');
        $('#themeJS').attr('src', 'js/theme/' + style + '.js');
        return false;
    })

lets assume if i click on the first color, the path would be changed to <script src="js/theme/theme-blue.js"></script>

假设我点击第一种颜色,路径将更改为

it's changing the path if i inspect but it's not giving me the effects which i want from theme-blue.js

它正在改变路径,如果我检查,但它没有给我从theme-blue.js我想要的效果

the js files theme-default.js and theme-blue.js

js文件theme-default.js和theme-blue.js

( theme-default.js )

(theme-default.js)

   $('body').css({
    'background-color':'#eee'
   });

( theme-blue.js )

(theme-blue.js)

     $('body').css({
    'background-color':'#3498db'
   });

i want to fix this thing with JS

我想用JS解决这个问题

1 个解决方案

#1


2  

Daniel -

As far as I know, you cannot replace runtime javascript by changing the src attribute. But what you are trying to do can be done much easier with CSS. Change the data-style to the color you want to see and use this script instead. See the example below:

据我所知,您无法通过更改src属性来替换运行时javascript。但是,使用CSS可以更轻松地完成您要做的事情。将数据样式更改为您要查看的颜色,然后使用此脚本。请参阅以下示例:

$('.color-changer a').on('click', function(){
    var style = $(this).attr('data-style');
        $('body').css('background-color', style);
    })

#1


2  

Daniel -

As far as I know, you cannot replace runtime javascript by changing the src attribute. But what you are trying to do can be done much easier with CSS. Change the data-style to the color you want to see and use this script instead. See the example below:

据我所知,您无法通过更改src属性来替换运行时javascript。但是,使用CSS可以更轻松地完成您要做的事情。将数据样式更改为您要查看的颜色,然后使用此脚本。请参阅以下示例:

$('.color-changer a').on('click', function(){
    var style = $(this).attr('data-style');
        $('body').css('background-color', style);
    })