[英]Javascript: onclick function works once then page reverts back to original
[英]Duplicated a JavaScript onclick function, only the original works?
我對這一切都是新手,正在嘗試使用附加的JavaScript onclick
函數復制圖像。 我已經復制了圖像和編碼,並將其放置在新頁面上,但是onclick
函數似乎不起作用。 但是原來的作品就很好。 原始的onclick
打開一個模式窗口,但是重復的窗口則什么也不做。
我想念什么?
這是帶有onclick
函數的圖像:
<img id="product-bump-<?php echo $product->ID; ?>" src="<?php echo get_template_directory_uri();?>/assets_consumer/assets/img/bump-icon_.png" width="20" class="product-arrow" data-toggle="tooltip" data-placement="left" title="Bump" onclick="javascript:bump_hype_product(<?php echo $product->ID; ?>, 'B')" />
這是位於我的consumer-global.js
文件中的函數:
function bump_hype_product(post_id, type_text){
jQuery('#product-message-confirmation-wrap').hide();
jQuery('.popup-waiting-wrap').show();
jQuery('#modal_product_message_confirmation h4.modal-title').html('');
jQuery.ajax({
url : the_ajax_script.ajaxurl,
type : 'post',
data : {
action : 'bump_hype_product',
type : type_text,
post_id : post_id
},
success : function( response ) {
jQuery('.popup-waiting-wrap').hide();
jQuery('#product-message-confirmation-wrap').show();
jQuery('#product-message-confirmation-wrap').html(response);
jQuery('#modal_product_message_confirmation').modal('show');
}
});
腳本文件列在我所有頁面的標題中。
當您使用javascript函數復制圖像時,該動態生成的圖像不會與HTML Dom綁定在一起,因此您會遇到此問題。 搜索Jquery的Delegate事件,並使用委托事件重組代碼。 它將工作!
嘗試這樣:
$('staticElement').on('click','#dynamicallyAddedElament',handlerFunctio);
這會將事件偵聽器添加到您的靜態元素中,以便可以綁定在該靜態div中添加的動態元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.