如何在搜索表单中插入跨度? (创世纪)
How to insert a span inside a search form? (Genesis)
当我调用 genesis_search_form()
时,它输出:
<form class="search-form">
<meta itemprop="target">
<input type="search">
<input type="submit">
</form>
但我希望它在内部生成 span
,例如:
<form class="search-form">
<meta itemprop="target">
<input type="search">
<span class="submit-icon"></span>
<input type="submit">
</form>
正在寻找更安全的替代品:
add_filter( 'genesis_search_form', 'my_search_button' );
function my_search_button( $form ) {
return str_replace(
'<input type="submit"',
'<span class="submit-icon"></span><input type="submit"',
$form
);
}
避免替换标签的开头。有什么想法吗?
如果允许您更改功能,请更改。如果没有,破解它!
$('form.search-form input[type=search]').after('<span class="submit-icon"></span>');
使用 DOMDocument!这是您的代码的工作版本:
https://3v4l.org/ats7D
<?php
// Create a DOM Document
$dom = new DomDocument();
// Load your HTML
$dom->loadHTML('<form class="search-form">
<meta itemprop="target">
<input type="search">
<input type="submit">
</form>');
// Create a new <span>
$span = $dom->createElement('span', 'hello');
// Grab the <input elements (we dont have an ID)
$inputs = $dom->getElementsByTagName('input');
// Add the <span> between the inputs
$inputs->item(0)->parentNode->insertBefore($span, $inputs->item(1));
// By default when you loadHTML(), it generates doctype, html, head, and body tags. remove them!
$dom->removeChild($dom->doctype);
$dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild);
// Finally get the HTML
$html = $dom->saveHTML();
// And output / return / whatever
echo $html;
如果您对 DOMDocument 没意见,这个版本更适合 HTML5。
add_filter( 'genesis_search_form', 'my_search_button' );
function my_search_button($form) {
$document = new DOMDocument();
$document->loadHTML($form);
$xpath = new DOMXPath($document);
$input = $xpath->query('//input[@type="submit"]');
$span = $document->createElement('span');
$span->setAttribute('class', 'sb-icon');
if ($input->length > 0) {
$input->item(0)->parentNode->insertBefore($span, $input->item(0));
}
$document->removeChild($document->doctype);
$document->replaceChild($document->firstChild->firstChild->firstChild, $document->firstChild);
$form_html = $document->saveHTML();
return $form_html;
}
当我调用 genesis_search_form()
时,它输出:
<form class="search-form">
<meta itemprop="target">
<input type="search">
<input type="submit">
</form>
但我希望它在内部生成 span
,例如:
<form class="search-form">
<meta itemprop="target">
<input type="search">
<span class="submit-icon"></span>
<input type="submit">
</form>
正在寻找更安全的替代品:
add_filter( 'genesis_search_form', 'my_search_button' );
function my_search_button( $form ) {
return str_replace(
'<input type="submit"',
'<span class="submit-icon"></span><input type="submit"',
$form
);
}
避免替换标签的开头。有什么想法吗?
如果允许您更改功能,请更改。如果没有,破解它!
$('form.search-form input[type=search]').after('<span class="submit-icon"></span>');
使用 DOMDocument!这是您的代码的工作版本: https://3v4l.org/ats7D
<?php
// Create a DOM Document
$dom = new DomDocument();
// Load your HTML
$dom->loadHTML('<form class="search-form">
<meta itemprop="target">
<input type="search">
<input type="submit">
</form>');
// Create a new <span>
$span = $dom->createElement('span', 'hello');
// Grab the <input elements (we dont have an ID)
$inputs = $dom->getElementsByTagName('input');
// Add the <span> between the inputs
$inputs->item(0)->parentNode->insertBefore($span, $inputs->item(1));
// By default when you loadHTML(), it generates doctype, html, head, and body tags. remove them!
$dom->removeChild($dom->doctype);
$dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild);
// Finally get the HTML
$html = $dom->saveHTML();
// And output / return / whatever
echo $html;
如果您对 DOMDocument 没意见,这个版本更适合 HTML5。
add_filter( 'genesis_search_form', 'my_search_button' );
function my_search_button($form) {
$document = new DOMDocument();
$document->loadHTML($form);
$xpath = new DOMXPath($document);
$input = $xpath->query('//input[@type="submit"]');
$span = $document->createElement('span');
$span->setAttribute('class', 'sb-icon');
if ($input->length > 0) {
$input->item(0)->parentNode->insertBefore($span, $input->item(0));
}
$document->removeChild($document->doctype);
$document->replaceChild($document->firstChild->firstChild->firstChild, $document->firstChild);
$form_html = $document->saveHTML();
return $form_html;
}