【问题标题】:Why does this code add id="active" to html?为什么这段代码将 id="active" 添加到 html?
【发布时间】:2009-09-30 09:43:04
【问题描述】:

下面的视图代码产生的html代码如下。

第一季度。第一行在做什么?

<?php $ci_uri = trim($this->uri->uri_string(), '/'); $att = ' id="active"';?>

第二季度。为什么

生产 id="active"?

第三季度。目的是什么

<?= substr($ci_uri, 0, 7) == 'example'? $att: ''?>
<?= $ci_uri == $this->config->item('FAL_login_uri')? $att: ''?>
...
...
<?= $ci_uri == $this->config->item('FAL_changePassword_uri')? $att: ''?>
etc.

是否使用三元运算符?

查看代码

<?php $ci_uri = trim($this->uri->uri_string(), '/'); $att = ' id="active"';?>
<ul id="navlist">
<li<?= $ci_uri == ''? $att: ''?>><?=anchor('', 'home')?></li>
<li<?= substr($ci_uri, 0, 7) == 'example'? $att: ''?>><?=anchor('example', 'examples')?></li>
<li<?= $ci_uri == $this->config->item('FAL_login_uri')? $att: ''?>><?=anchor($this->config->item('FAL_login_uri'), 'login')?></li>
<li<?= $ci_uri == $this->config->item('FAL_register_uri')? $att: ''?>><?=anchor($this->config->item('FAL_register_uri'), 'register')?></li>
<li<?= $ci_uri == $this->config->item('FAL_forgottenPassword_uri')? $att: ''?>><?=anchor($this->config->item('FAL_forgottenPassword_uri'), 'forgotten password')?></li>
<li<?= $ci_uri == $this->config->item('FAL_changePassword_uri')? $att: ''?>><?=anchor($this->config->item('FAL_changePassword_uri'), 'change password')?></li>
<li<?= substr($ci_uri, 0, 5) == 'admin'? $att: ''?>><?=anchor('admin', 'admin')?></li>
</ul>

HTML 代码

<ul id="navlist">
<li id="active"><a href="http://127.0.0.1/ci_freak_auth/index.php">home</a></li>
<li><a href="http://127.0.0.1/ci_freak_auth/index.php/example.html">examples</a></li>
<li><a href="http://127.0.0.1/ci_freak_auth/index.php/auth/login.html">login</a></li>
<li><a href="http://127.0.0.1/ci_freak_auth/index.php/auth/register.html">register</a></li>
<li><a href="http://127.0.0.1/ci_freak_auth/index.php/auth/forgotten_password.html">forgotten password</a></li>
<li><a href="http://127.0.0.1/ci_freak_auth/index.php/auth/changepassword.html">change password</a></li>
<li><a href="http://127.0.0.1/ci_freak_auth/index.php/admin.html">admin</a></li>
</ul>

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:
    1. 它将 $ci_uri 设置为当前 URL,删除了前导和尾随斜杠,因此“/foo/bar/”将变为“foo/bar”。然后将 $att 设置为该字符串,以供以后使用。

    2. 该字符串稍后将被注入到 HTML 标记中 - 可能因此可以使用 CSS 设置不同的样式

    3. 基本上它正在查看它正在打印的链接是否是当前 URL,如果是,则添加 active 属性。

    总之,就是要对指向当前页面的链接设置不同的样式。

    代码:

    <?= $ci_uri == $this->config->item('FAL_login_uri')? $att: ''?>
    

    意思是:

    <?= // this is a shortcut for <?php echo
    
    // If the current url is the same as the url for "FAL_login_uri"
    $ci_uri == $this->config->item('FAL_login_uri')
    
    // then
    ?
    
    // use $att in the echo
    $att
    
    // else
    :
    
    // use an empty string in the echo
    ''
    ?>
    

    $x ? $y : $z 语法是 ternary operator - 这几乎是 if / else 的一种简短方式

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-17
      • 2018-09-01
      • 2015-07-01
      相关资源
      最近更新 更多