te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Using ternary operator in HTML to decide on the class to be implemented - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using ternary operator in HTML to decide on the class to be implemented - Stack Overflow

programmeradmin3浏览0评论

I want to decide on a CSS class dynamically using ternary operator in HTML. I'm using following code but it seems the condition isn't being evaluated at all. Whatever class that is written in the true part of the condition, is applied. How can I apply a particular class based on some condition? Any alternative approach will be helpful too.

<td class="col-md-6"><span class="form-control-static cdr-details-td" />
  <span class="(('1' === '-1') ? version-modal-header : failure)">{{cdr.causeCode}}</span> 
</td>

CSS for the same:

.version-modal-header {
  background-color: #000000;
}
.failure {
  color: #ff0000;
  font-weight: bold;
}

I want to decide on a CSS class dynamically using ternary operator in HTML. I'm using following code but it seems the condition isn't being evaluated at all. Whatever class that is written in the true part of the condition, is applied. How can I apply a particular class based on some condition? Any alternative approach will be helpful too.

<td class="col-md-6"><span class="form-control-static cdr-details-td" />
  <span class="(('1' === '-1') ? version-modal-header : failure)">{{cdr.causeCode}}</span> 
</td>

CSS for the same:

.version-modal-header {
  background-color: #000000;
}
.failure {
  color: #ff0000;
  font-weight: bold;
}
Share Improve this question edited Jun 7, 2016 at 4:58 James Vu 2,4713 gold badges15 silver badges33 bronze badges asked Jun 7, 2016 at 4:30 NattyNatty 551 gold badge1 silver badge10 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 5

Use ng-class instead

ng-class="{'version-modal-header': condition, 'failure': !condition}"

You can also use a ternary if you want

ng-class="condition ? 'version-modal-header' : 'failure'"

Please check working example here : Example

Use ng-class

 ng-class="{true: 'version-modal-header', false: 'failure'}[changeClass]" 

You can write your conditions in [changeClass]

if you looking for ternary operator in style attribute in view pages then following answer will work for you.

style ="background-color: @(Model.IsVisible ? "#fde0e0;" : "#DFF0D8;")"

The ternary operation that you have incorporated in your program, doesn't works for class or to be specific - HTML DOM. It can be done using AngularJS, as it provides a wonderful inbuilt directive knows as ng-class. Using this one can set the conditions, based on which one needs to apply the class.

Have a look at the snippet below:

<td class="col-md-6"><span class="form-control-static cdr-details-td" />
    <span ng-class="{'version-modal-header': '1' === '-1', 'failure': '1' === '1'}">{{cdr.causeCode}}</span>
</td>

Check out the demo here.

Use ng-class if you want to apply a css class dynamically. dont use interpolation or binding {{}} to apply css class directly as it will not work across browsers. In ng-class you just need to specify an object containing class names as keys and the respective Boolean condition as values. Any class whose value evaluates to true is applied dynamically.

<td class="col-md-6"><span class="form-control-static cdr-details-td" />
  <span ng-class="{'version-modal-header':('1' === '-1') , 'failure': ('1' !== '-1')}">{{cdr.causeCode}}</span> 
</td>

https://docs.angularjs/api/ng/directive/ngClass

you can use it to show and hide content dynamically

{{condition ? 'value if true' : 'value if false'}}

Heres a solution that worked for me.

<p style={{borderBottom: loginToggle ? '6px solid blue' : "None"}}></p>
发布评论

评论列表(0)

  1. 暂无评论