【问题标题】:Jquery: Input type="button"with .click() function dont work in Internet Explorer and SafariJquery:带有 .click() 功能的输入类型 =“按钮”在 Internet Explorer 和 Safari 中不起作用
【发布时间】:2016-04-29 09:59:58
【问题描述】:

如何使用jquery 函数.click() 使输入type="button" 起作用 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>

【问题讨论】:

  • 你能粘贴生成的 HTML 而不是 php 代码吗?

标签: javascript jquery html safari explorer


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    这应该适合您的目的:

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

    我的补充是:

    改用data() JQuery 函数

    等待文件准备好

    防止默认行为

    【讨论】:

    • 如果我使用data() 而不是attr(),那么它不适用于所有浏览器。 &lt;script type="text/javascript"&gt; $(document).ready(function(){ $(".CommentsSection").click(function(ev){ ev.preventDefault(); var sectionID=$(this).data('data-id'); $("#"+sectionID).show(); }); }); &lt;/script&gt;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    相关资源
    最近更新 更多