javascript - 将类添加到点击的模态

标签 javascript php

foreach ( $Select as $List ) {

   print $HTML = '

       <button id="btn_'. $this->ID .'" onclick="Modal( '. $this->ID .', '. 
       $this->ID .' )">'. $Name .'</button>

   ';

   // My modal
   print $HTML = '<div id="modal_'. $this->ID .'" class="modal fix">

      echo $this->ID;

   </div>';

}

echo '
    <script>
        function Modal( IDModal, IDBtn ) {

           const MyBtn   = document.getElementById( "btn_" + id ).id;

           const MyModal = document.getElementById( "modal_" + IDModal ).id;

           MyModal.classList.add("show");

        }
     </script>
';

当我单击按钮打开其派生模式时,出现以下错误

Uncaught TypeError: Cannot read property 'add' of undefined

最佳答案

echo '
    <script>
        function Modal( IDModal, IDBtn ) {
           // remove .id and use  `IDBtn` also
           const MyBtn   = document.getElementById( "btn_" + IDBtn );
           // remove .id
           const MyModal = document.getElementById( "modal_" + IDModal );
           MyModal.classList.add("show");
        }
     </script>
';

您还可以通过使用 ?> 关闭 php-tag 并使用纯 html 标记和 js 代码来简化输出:

// closing previous php-code
?>
<!-- Simple output of tags and js-codes -->
<script>
    function Modal( IDModal, IDBtn ) {
        // remove .id and use  `IDBtn` also
        const MyBtn   = document.getElementById( "btn_" + IDBtn );
        // remove .id
        const MyModal = document.getElementById( "modal_" + IDModal );
        MyModal.classList.add("show");
    }
</script>

关于javascript - 将类添加到点击的模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58574661/

相关文章:

php - 视频托管 - 压缩视频?

php - 构建支持 ajax 的应用程序的最佳方式

javascript - 通过函数更新服务中的 'this' 值

javascript - 从 javascript 中的 for() 中排除一个以 'ElementByName' 结尾的数组

javascript - React redux-saga 如何将 prop 传递给嵌套子级

PHP4 到 PHP5 迁移字符编码。 latin1 与 utf8

javascript - 输入清理 Laravel 4

javascript - 从子窗口给出父窗口的 href url 位置

javascript - JQuery Columnizer 在一行中有几个 Columnizer 对象。横向扩展...如何?

php - 如何使用 AJAX、PHP 和 MySQL 使过滤器协同工作