【问题标题】:How can I re-formating PHP code, returning duplicate attribute class?如何重新格式化 PHP 代码,返回重复的属性类?
【发布时间】:2021-01-21 17:23:23
【问题描述】:

亲爱的,我想请你帮忙格式化 PHP 代码。 因为 W3C 验证器,所以将其作为错误返回给我,“错误:重复属性类。” 我现在有下一个 PHP 代码:

$html .= '<a href="'.$href.'"';
if($this->hasChilds($item, $nazev)) $html .= ' class="qmparent"';
if ((empty($view) && $item->view == 'main') || (empty($display) && $view == $item->view) || (!empty($display) && $display == $item->display)) $html .= ' class="active"';
$html .= '>';

这给了我 html
代码:&lt;a href="link.html" class="qmparent" class="active"&gt;LINK&lt;/a&gt; 我想,可以生成, 不同的 html
代码:&lt;a href="link.html" class="qmparent active" &gt;LINK&lt;/a&gt;
能否请教一下,如何改写?我不知道它是否足以正确理解,如果没有,我会添加很多代码。谢谢!

【问题讨论】:

    标签: php class


    【解决方案1】:

    错误是 HTML 错误,因为您有重复的类。在 HTML 中,可以有空的类属性。因此,您可以附加类,然后将它们添加到 HTML 类属性。见代码:

    $classes = '';
    if($this->hasChilds($item, $nazev)) $classes .= ' qmparent';
    if ((empty($view) && $item->view == 'main') || (empty($display) && $view == $item->view) || (!empty($display) && $display == $item->display)) $classes .= ' active';
    $html .= '<a href="'.$href.'"class ="'.$classes.'"/>';
    

    为了获得更好的结构,通常将视图与模型分开。因此,您可以创建一个包含最终类、类型和 id 的 HTML 元素的数据数组。然后将这个数组传递给 view 以正确构造视图。

    【讨论】:

    • 非常感谢您的建议,效果很好!我想我完全失明或失眠' :-) 这是一个非常古老的项目,我刚刚经历了它并希望一切都有效。我需要踢我。再次感谢,祝您玩得愉快。
    • @user3452734 欢迎您,如果答案确实解决了您可以标记为已接受的问题。
    • 我想发送“已接受”,但很抱歉,我没有找到如何发送,即使我确实阅读了帮助。
    猜你喜欢
    • 1970-01-01
    • 2019-08-20
    • 2012-04-24
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    • 2011-04-20
    • 2011-12-30
    • 2017-12-11
    相关资源
    最近更新 更多