Jquery:带有.click()函数的输入类型=“按钮”在Internet Explorer和Safari中不起作用

时间:2023-01-17 08:35:11

How to make input type="button" with jquery function .click() work in Internet Explorer and Safari? It works fine in Mozilla, Chrome, and Opera. Example, wenn button is clicked hidden div is showed:

如何使用jquery函数生成输入类型=“按钮”.click()在Internet Explorer和Safari中工作?它在Mozilla,Chrome和Opera中运行良好。例如,单击wenn按钮隐藏div显示:

<input type="button" value="Comments(<?php echo $commentCounter?>)" data-id="<?php echo $productArray[$key]["ID"].$_SESSION["userSession"];?>" class="CommentsSection"/>
<div id="<?php echo $productArray[$key]["ID"].$_SESSION["userSession"];?>" style="display:none">
<?php
echo"This is hidden div";
?>
</div>

JavaScript:

<script type="text/javascript">
$(document).ready(function(){

$(".CommentsSection").click(function(){
    var sectionID=$(this).attr('data-id');
$("#"+sectionID).show();
});
});
</script>

2 个解决方案

#1


0  

Most likely your JS is loaded and executed before your HTML is rendered. You could enclose your JS in a documentReady event (and close your onclick parenthesis) :

很有可能在呈现HTML之前加载并执行了JS。您可以将JS包含在documentReady事件中(并关闭onclick括号):

$(document).ready(function() {
   $(".CommentsSection").click(function(){
      var sectionID=$(this).attr('data-id');
      $("#"+sectionID).show();
   });
});

#2


0  

This should suit your purposes:

这应该适合您的目的:

$(document).ready(function() {
   $(".CommentsSection").click(function(ev){
      ev.preventDefault();

      var sectionID=$(this).data('id');
      $("#"+sectionID).show();
   });
});

My additions are:

我的补充是:

Use the data() JQuery function instead

请改用data()JQuery函数

Wait for document ready

等待文件准备好了

Prevent the default behaviour

防止默认行为

#1


0  

Most likely your JS is loaded and executed before your HTML is rendered. You could enclose your JS in a documentReady event (and close your onclick parenthesis) :

很有可能在呈现HTML之前加载并执行了JS。您可以将JS包含在documentReady事件中(并关闭onclick括号):

$(document).ready(function() {
   $(".CommentsSection").click(function(){
      var sectionID=$(this).attr('data-id');
      $("#"+sectionID).show();
   });
});

#2


0  

This should suit your purposes:

这应该适合您的目的:

$(document).ready(function() {
   $(".CommentsSection").click(function(ev){
      ev.preventDefault();

      var sectionID=$(this).data('id');
      $("#"+sectionID).show();
   });
});

My additions are:

我的补充是:

Use the data() JQuery function instead

请改用data()JQuery函数

Wait for document ready

等待文件准备好了

Prevent the default behaviour

防止默认行为