So I have been given a problem where with given code, I need to replace an a
tag within an li
tag for navigation with a span
tag.
For example, I need to have something like this:
<li>
<a href="example.com">Link text</a>
</li>
Look like this:
<li>
<span>Link Text</span>
</li>
Using php to replace the a
with span
.
Would anyone be able to tell me what might be able to complete this? I can't use javascript and only php.
To be clear this is my understanding of what needs to be done to complete this task. If someone has a better way of doing this I'd appreciate the help. Thank you.
This is what I currently have
<ul class="page-content-nav">
<li>
<a class="nav-selected" href="#">Programs</a>
</li>
<li>
<a class="" href="#">Programs</a>
</li>
</ul>
So I need the <a>
within to be replaced with a <span>
when it gains the class "nav-selected" which only happens when I'm on that particular page.
Hopefully this can help clarify
答案 0 :(得分:0)
如果我正确理解了您的问题,那么当您满足某些条件时,您会尝试将<a>
标记替换为<span>
标记。然后你解释说,条件是当用户点击链接时。如果这是正确的,你可以这样做:
<li>
<?php
//variable storing the class name
$className = "nav-selected";
// do all of the DOMElement operations here to determine what the class is. For this, look at the link provide in the comments
$actualClassName = "className";
if($className == $actualClassName)
?>
<span>Link Text</span>
<?php
else if($someCondition == false)
?>
<a href="example.com">Link text</a>
</li>
答案 1 :(得分:0)
<强>方法1 强>
如果您的目标是更改属性锚点 - 删除悬停和点击,您可以使用以下内容:
<style type="text/css">
.nav-selected{
cursor:default;
pointer-events: none;
}
</style>
<ul class="page-content-nav">
<li>
<a class="nav-selected" href="#">Programs</a>
</li>
<li>
<a class="" href="#">Programs</a>
</li>
</ul>
方法2
以下不是实际代码,只是一个想法。
<?php
echo '<ul class="page-content-nav">';
while/for(Loop through your array or mysql)
{
$Link = $array['link']; //eg: 'http://something.page.php'
$Text = $array['menu_name']; //eg: programs
$Id = $array['id'];
if ($Id == $current_page_id)
{
echo '
<li>
<span class="nav-selected" href="#">'.$Text.'</span>
</li>
';
}
else
{
echo '
<li>
<a class="" href="'.$Link.'">'.$Text.'</a>
</li>
';
}
}
echo '</ul>';
?>
答案 2 :(得分:0)
我做了一个这样做的课程:
class LiBuilder{
public function buildListElement($text,$href=null){
$li_inner_html = $this->buildListElementInnerHtml($text,$href);
$li = $this->wrapInLi($li_inner_html);
return $li;
}
private function buildListElementInnerHtml($text,$href){
if($href)
$li_inner_html = $this->buildAnchorElement($text,$href);
else
$li_inner_html = $this->buildSpanElement($text);
return $li_inner_html;
}
private function wrapInLi($inner_html){
return "<li>{$inner_html}</li>";
}
private function buildAnchorElement($href,$text){
return "<a href=\"{$href}\">{$text}</a>";
}
private function buildSpanElement($text){
return "<span>{$text}</span>";
}
}
然后,您可以像这样使用它:
$li_builder = new LiBuilder();
$li_1 = $li_builder->buildListElement('Here is a magic element without link');
$li_2 = $li_builder->buildListElement(
'Another magic element but with link this time.',
'http://www.stackoverflow.com'
);
echo $li_1, '<br />', $li_2;
所以,只需复制/粘贴我的LiBuilder类,然后需要它,然后调用方法buildListElement。
它可能过度设计,但至少它是可测试的并且相对干净。不要忘记检查XSS。