gpt4 book ai didi

javascript - 在 WordPress 主导航中选择一个 href 以打开模式

转载 作者:太空宇宙 更新时间:2023-11-04 15:33:06 26 4
gpt4 key购买 nike

我正在创建 WordPress 主题,并尝试通过单击导航菜单内包含 a hrefli 元素来打开模式

我的代码在静态 html 中运行:

// Get the modal
var modal1 = document.getElementById("myModal-registration");

// Get the button that opens the modal
var btn1 = document.getElementById("sign-up");

// Get the <span> element that closes the modal
var span1 = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal
btn1.onclick = function() {
modal1.style.display = "block";
};

// When the user clicks on <span> (x), close the modal
span1.onclick = function() {
modal1.style.display = "none";
};


// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal1) {
modal1.style.display = "none";
}
};
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
z-index: 1000;
}


/* Modal Content */
.modal-content {
background-color: #fefefe;
margin: 0 auto;
padding: 20px;
border: 1px solid #888;
width: 60%;
@media screen and (max-width: 420px) {
width: 100%;
}
.container {
font-family: 'Open Sans', sans-serif;
display: block;
font-size: 18px;
line-height: 20px;
// margin: 0 auto;
}
}

/* The Close Button */
.close {
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
}

.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
 <nav class="navBar">
<nav class="wrapper">
<ul>
<li class="pure-menu-link"><a href="#sign-in" id="sign-up">SIGN UP</a></li>
</ul>
</nav>
</nav>




<div id="myModal-registration" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">&times;</span>
<form class="modal-content-animate" action="/action_page.php">
<div class="container">
<!--<label><b>Email</b></label>-->
<input type="text" placeholder="Enter Email" name="email" required>
<br>
<input type="password" placeholder="Enter Password" name="psw" required>
<br>
<input type="password" placeholder="Repeat Password" name="psw-repeat" required>
<br>
<div class="clearfix">
<button type="submit" class="signupbtn">Sign Up</button>
<button type="button" onclick="document.getElementById('myModal-registration').style.display='none'" class="cancelbtn">Cancel</button>
</div>
</div>
</form>
</div>
</div>

但是在 WordPress 中,菜单是在 functions.php 中注册的:

function register_theme_menus() {

register_nav_menus(array(
'primary-navigation' => __( 'Primary Navigation' ),
));
}

并在 header.php 中使用 PHP 进行调用:

   wp_nav_menu(array(
'container' => 'ul',
'menu_class' => '',
'theme_location' => 'primary-navigation',
'menu' => 'primary-navigation'
));

毕竟,生成的 HTML 代码没有 a href 的 id:

<ul id="menu-main-navigation" class="">
<li id="menu-item-20" class="pure-menu-link menu-item menu-item-type-custom menu-item-object-custom menu-item-20"><a href="#sign-up">SIGN UP</a></li>
</ul>

在 WordPress 菜单选项中设置了带有 # 的自定义 URL: enter image description here

并尝试使用 JS 将其选为 a href:

var btn1 = document.querySelectorAll("a[href^='#sign-up']");

但没有任何像样的结果。我是否在这里遗漏了一点,在这种情况下仅使用 JS 不是一个有效的选项?如果您还有任何其他问题,请告诉我。非常感谢所有可能的帮助,将期待。

最佳答案

wp 菜单中的菜单项 (li) 通常具有唯一的类。在你的情况下是 menu-item-20您可以使用 .menu-item-20 a 定位您所询问的链接所以使用

// Get the button that opens the modal
var btn1= document.querySelector(".menu-item-20>a")

关于javascript - 在 WordPress 主导航中选择一个 href 以打开模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44638387/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com