如何更改点击功能添加的img src?

How to change img src added by on click function?

我想更改外部添加的 _.js 的变量 var ajax_loader = jQuery('<img class="fl-spinner" width="16" heigth="16" src="' + fl_mc_settings.fl_spinner + '" />');,但我无法更改、删除等

jQuery(document).ready(function() {
    jQuery(document).on("click", ".fl_click_switcher a", fl_switch_currency_handler)
});
var fl_switch_currency_handler = function(event) {
    event.preventDefault();
    if (jQuery(this).is(":disabled") || jQuery(this).parent().hasClass("fl-cs-active-currency") || jQuery(this).hasClass("fl-cs-active-currency")) {
        return false
    } else {
        jQuery(this).off(event)
    }
    fl_load_currency(jQuery(this).attr("rel"))
};
function fl_load_currency(currency, force_switch) {
    var ajax_loader = jQuery('<img class="fl-spinner" width="16" heigth="16" src="' + fl_mc_settings.fl_spinner + '" />');
    jQuery(".fl_click_switcher").append(ajax_loader);
    if (typeof force_switch === "undefined")
        force_switch = 0;
    jQuery.ajax({.........

谢谢你

最简单的解决方案必须是将 fl_mc_settings.fl_spinner = "new-image.jpg"; 添加到您的脚本中(确保它在微调器 js 之后加载)

有了它,只需覆盖用作 spinner

的图像

例如

<script src="path-to-spinner.js"></script>
<script>
    fl_mc_settings.fl_spinner = "new-image.jpg";
</script>

根据评论更新

由于图像源被注入到 HTML 片段中,这里有一个使用 imgonload 事件的技巧。

它所做的也是将 onload="wrap_img(this);" 注入到 img 标记中,当插入图像并加载图像时,事件会触发,然后就可以了,到这里就完成了使用 jQuery 的 .wrap() 方法,将图像包裹在例如div.

var fl_spinner = 'http://placehold.it/200x100/" onload="wrap_img(this);"';
var ajax_loader = '<img class="fl-spinner" width="160" heigth="160" src="' + fl_spinner + '" />';

function wrap_img(img) {
  $(img).wrap( "<div class='spinner_wrapper'></div>" );
}

document.body.innerHTML = ajax_loader;
.spinner_wrapper {
  display: inline-block;
  background: red;
  padding: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


在你的情况下,它可能看起来像这样

<script src="path-to-spinner.js"></script>
<script>
    fl_mc_settings.fl_spinner = 'new-image.jpg" onload="wrap_img(this);"';

    function wrap_img(img) {
      jQuery(img).wrap( "<div class='spinner_wrapper'></div>" );
    }
</script>
<style>
    .spinner_wrapper {
      display: inline-block;
      background: red;
      padding: 20px;
    }
</style>