我想使用添加操作将自定义覆盖搜索添加到我的WordPress站点。不知何故,它并没有触发javascripts来显示覆盖html。然而,当我通过保存为html在浏览器中进行测试时,它在浏览器中工作,can be checked here. 如何正确地将它们添加到n个函数中。php来运行这些代码。
这是我添加到函数中的代码。php。甚至,当页面加载时,脚本也会出现。searchbox包含onclick事件。
表单代码
<form method="get" id="search" action="https://tatest.tutorialsart.com/">
<input id="search-input" inputmode="search" onclick="openSearch()" type="text" name="s" title="Search for" placeholder="Search for" />
<button id="search-submit" type="submit">
<span class="tie-icon-search tie-search-icon" aria-hidden="true"></span>
<span class="screen-reader-text">Search for</span>
</button>
</form>
用于在函数中触发覆盖的脚本。php
add_action( \'wp_footer\', \'Search_trigger_script\' );
function Search_trigger_script() {
?>
<script type="text/javascript">
function openSearch() {
document.getElementById("myOverlay").style.display = "block";
}
function closeSearch() {
document.getElementById("myOverlay").style.display = "none";
}
</script>
<?php
}
函数中的html覆盖代码。php
add_action( \'wp_footer\', \'Search_overlay_html\' );
function Search_overlay_html() {
?>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
body {
font-family: Arial;
}
* {
box-sizing: border-box;
}
.openBtn {
background: #f1f1f1;
border: none;
padding: 10px 15px;
font-size: 20px;
cursor: pointer;
}
.openBtn:hover {
background: #bbb;
}
.overlay {
height: 100%;
width: 100%;
display: none;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0, 0.9);
}
.overlay-content {
position: relative;
top: 2%;
width: 80%;
text-align: center;
margin-top: 30px;
margin: auto;
}
.overlay .closebtn {
position: absolute;
top: 20px;
right: 45px;
font-size: 60px;
cursor: pointer;
color: white;
}
.overlay .closebtn:hover {
color: #ccc;
}
.overlay input[type=text] {
padding: 15px;
font-size: 17px;
border: none;
float: left;
width: 80%;
background: white;
}
.overlay input[type=text]:hover {
background: #f1f1f1;
}
.overlay button {
float: left;
width: 20%;
padding: 15px;
background: #ddd;
font-size: 17px;
border: none;
cursor: pointer;
}
.overlay button:hover {
background: #bbb;
}
</style>
</head>
<body>
<div id="myOverlay" class="overlay">
<span class="closebtn" onclick="closeSearch()" title="Close Overlay">×</span>
<div class="overlay-content">
<form action="/action_page.php">
<input type="text" placeholder="Search.." name="search">
<button type="submit"><i class="fa fa-search"></i></button>
</form>
</div>
</div>
</body>
</html>
<?php
}
最合适的回答,由SO网友:Kanjoo 整理而成
添加了ajax脚本
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
覆盖的z指数必须高于任何其他样式。onclick事件正在运行,但结果未显示。将z指数值设置得更高表明结果有效。所以
.overlay {
height: 100%;
width: 100%;
display: none;
position: fixed;
z-index: 999;
top: 0;
left: 0;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0, 0.9);
}
SO网友:Martin Mirchev
首先,您需要编辑searchform。php文件。如果您使用的是get\\u search\\u form();read this . 如果使用搜索小部件,则需要覆盖该小部件。
如果您不想使用searchform。php您可以使用过滤器:
function wpdocs_my_search_form( $form ) {
$form = \'<form role="search" method="get" id="searchform" class="searchform" action="\' . home_url( \'/\' ) . \'" >
<div><label class="screen-reader-text" for="s">\' . __( \'Search for:\' ) . \'</label>
<input type="text" value="\' . get_search_query() . \'" name="s" id="s" />
<input type="submit" id="searchsubmit" value="\'. esc_attr__( \'Search\' ) .\'" />
</div>
</form>\';
return $form;
}
add_filter( \'get_search_form\', \'wpdocs_my_search_form\' );
之后,您的wordpress搜索表单应包含onclick=";openSearch();。您可以使用二十二个主题作为示例。我已经测试了您的代码及其工作情况。请参见屏幕截图-
https://prnt.sc/1qloaxz https://prnt.sc/1qlof07我只想使用JS或jQuery来定位覆盖表单。
JS
<script>
window.onload = function() {
var element = document.getElementsByClassName(\'target-class\');
element.onclick = function() {
alert(\'Success\');
}
}
</script>
jQuery
<script>
jQuery(document).ready(function() {
jQuery(\'.target-class\').click(function() {
alert(\'Success\');
});
});
</script>