這是我第一次在這里發帖,我對編碼還比較陌生,所以請原諒我的無知。我正在為我的 WordPress 模板開發一個頁面,該頁面將在單擊 div 時打開一個模式窗口以顯示圖像。這是到目前為止我的 HTML..<?php/*** Template Name: Home Page*/get_header(); ?><div class="grid-container"><?php$args = array('post_type' => 'artists',);$the_query = new WP_Query( $args );if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <?php $postId = get_the_ID(); ?> <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <div id="popup"> <img id="myImg" src="wp-content/themes/dollarartclub/images/women.png" alt="Snow" style="width:100%;max-width:300px"> </div><!-- modal --> <div id="mymodal" class="modal"> <span class="close">×</span> <img class="modal-content" id="img01"> <div id="caption"></div> </div> </article> <?php endwhile;endif;wp_reset_query(); ?> </article></div>Javascript(在我的functions.php中排隊)jQuery(document).ready(function($) {var modal = document.getElementById("myModal");var img = document.getElementById("myImg");var modalImg = document.getElementById("img01");var captionText = document.getElementById("caption");img.onclick = function(){ modal.style.display = "block"; modalImg.src = this.src; captionText.innerHTML = this.alt;}var span = document.getElementsByClassName("close")[0];span.onclick = function() { modal.style.display = "none";}});我不斷收到控制臺錯誤“未捕獲的 TypeError:無法訪問屬性“樣式”,模態為空”我收到“無法訪問 onclick,img 為空”,但將 javascript 包裝在 jquery 函數中似乎可以阻止此操作。我已經嘗試讓它工作一周了,但沒有成功,這讓我發瘋。任何幫助將不勝感激。謝謝你!
1 回答

慕斯王
TA貢獻1864條經驗 獲得超2個贊
你的選擇器是不同的,一個有一個大寫的M。
<div id="mymodal" class="modal">
document.getElementById("myModal")
- 1 回答
- 0 關注
- 120 瀏覽
添加回答
舉報
0/150
提交
取消