'tag.htm'; break; case 'flag': $pre .= $default_pre .= 'flag.htm'; break; case 'my': $pre .= $default_pre .= 'my.htm'; break; case 'my_password': $pre .= $default_pre .= 'my_password.htm'; break; case 'my_bind': $pre .= $default_pre .= 'my_bind.htm'; break; case 'my_avatar': $pre .= $default_pre .= 'my_avatar.htm'; break; case 'home_article': $pre .= $default_pre .= 'home_article.htm'; break; case 'home_comment': $pre .= $default_pre .= 'home_comment.htm'; break; case 'user': $pre .= $default_pre .= 'user.htm'; break; case 'user_login': $pre .= $default_pre .= 'user_login.htm'; break; case 'user_create': $pre .= $default_pre .= 'user_create.htm'; break; case 'user_resetpw': $pre .= $default_pre .= 'user_resetpw.htm'; break; case 'user_resetpw_complete': $pre .= $default_pre .= 'user_resetpw_complete.htm'; break; case 'user_comment': $pre .= $default_pre .= 'user_comment.htm'; break; case 'single_page': $pre .= $default_pre .= 'single_page.htm'; break; case 'search': $pre .= $default_pre .= 'search.htm'; break; case 'operate_sticky': $pre .= $default_pre .= 'operate_sticky.htm'; break; case 'operate_close': $pre .= $default_pre .= 'operate_close.htm'; break; case 'operate_delete': $pre .= $default_pre .= 'operate_delete.htm'; break; case 'operate_move': $pre .= $default_pre .= 'operate_move.htm'; break; case '404': $pre .= $default_pre .= '404.htm'; break; case 'read_404': $pre .= $default_pre .= 'read_404.htm'; break; case 'list_404': $pre .= $default_pre .= 'list_404.htm'; break; default: $pre .= $default_pre .= theme_mode_pre(); break; } if ($config['theme']) { $conffile = APP_PATH . 'view/template/' . $config['theme'] . '/conf.json'; $json = is_file($conffile) ? xn_json_decode(file_get_contents($conffile)) : array(); } !empty($json['installed']) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . ($id ? $id . '_' : '') . $pre; (empty($path_file) || !is_file($path_file)) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . $pre; if (!empty($config['theme_child']) && is_array($config['theme_child'])) { foreach ($config['theme_child'] as $theme) { if (empty($theme) || is_array($theme)) continue; $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . ($id ? $id . '_' : '') . $pre; !is_file($path_file) and $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . $pre; } } !is_file($path_file) and $path_file = APP_PATH . ($dir ? 'plugin/' . $dir . '/view/htm/' : 'view/htm/') . $default_pre; return $path_file; } function theme_mode_pre($type = 0) { global $config; $mode = $config['setting']['website_mode']; $pre = ''; if (1 == $mode) { $pre .= 2 == $type ? 'portal_category.htm' : 'portal.htm'; } elseif (2 == $mode) { $pre .= 2 == $type ? 'flat_category.htm' : 'flat.htm'; } else { $pre .= 2 == $type ? 'index_category.htm' : 'index.htm'; } return $pre; } ?>javascript - jQuery: How to check if an element exists and change css property - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery: How to check if an element exists and change css property - Stack Overflow

programmeradmin23浏览0评论

First of all, sorry if this is a simple question. I am new to jQuery and I want to know how can I check if an element exists and if it does, change the css property.

Here is what I mean: I have the following list:

<ul class="element-rendered">
    <li class="element-choice">Item A</li>
    <li class="select-inline">Item B</li>
</ul>

I want to know how can I check if the class select-inline exists inside element-rendered and if it does, how can I change the css background of element-choice to blue?

I created a fiddle to reproduce this example.

Again sorry if this is a simple question but I am new to jQuery.

First of all, sorry if this is a simple question. I am new to jQuery and I want to know how can I check if an element exists and if it does, change the css property.

Here is what I mean: I have the following list:

<ul class="element-rendered">
    <li class="element-choice">Item A</li>
    <li class="select-inline">Item B</li>
</ul>

I want to know how can I check if the class select-inline exists inside element-rendered and if it does, how can I change the css background of element-choice to blue?

I created a fiddle to reproduce this example.

Again sorry if this is a simple question but I am new to jQuery.

Share Improve this question asked Oct 7, 2015 at 14:42 brunoddbrunodd 2,58412 gold badges45 silver badges68 bronze badges 3
  • 1 if($('.element-rendered .select-inline').length) { $('.element-choice').css('background', 'blue'); } Check updated fiddle – Tushar Commented Oct 7, 2015 at 14:44
  • Hey @Tushar thank you again! You are always helping me here! haha! That worked. Would you mind answer that so I can mark as correct? – brunodd Commented Oct 7, 2015 at 14:47
  • $('.element-rendered .select-inline').css("background-color", "red") will (almost) be a no-op if the element is not found in the DOM. You could go without checking the existance at all – Andreas Commented Oct 7, 2015 at 14:48
Add a ment  | 

6 Answers 6

Reset to default 3

You can use .length to check if the element exists in DOM.

$('.element-rendered .select-inline') will select all the elements having class select-inline inside the element with class element-rendered. .length on selector will return the number of matched elements. So, number greater that one, means the element exists. Then you can use .css to set inline styles.

Demo

if ($('.element-rendered .select-inline').length) {
  $('.element-choice').css('background', 'blue');
}
.element-choice {
  width: 100%;
  background: red;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<ul class="element-rendered">
  <li class="element-choice">Item A</li>
  <li class="select-inline">Item B</li>
</ul>


I'll also remend you to use class in CSS and add it to the element by using addClass.

Demo

var eR = $(".element-rendered");
if (eR.find(".select-inline").length > 0){
  eR.find(".element-choice").css("color", "blue");
}

This would work for your specific example.

Find the select-line element which is a child of element-rendered. The find all of the sibling elements with class element-choice and apply the css.

$('.element-rendered>.select-inline').siblings('.element-choice').css('background','blue')

http://jsfiddle/SeanWessell/hjpng78s/3/

To check if element exists could use .is() , or as suggested by @Tushar .length

var container = $(".element-rendered");
// alternatively `!!$(".select-inline", container).length`
$(".select-inline", container).is("*") 
&& $(".element-choice", container).css("background", "blue");

jsfiddle http://jsfiddle/hjpng78s/6/

Demo

if($(".element-rendered .select-inline")[0])
    $(".element-rendered .select-inline").css("background-color","red");

How can I check if the class select-inline exists inside element-rendered and if it does, how can I change the css background of element-choice to blue?

if ( $(".element-rendered > .select-inline").length ) {
    $(".element-rendered > .element-choice").css({
        'background-color': 'blue'
    });
}

Docs:

  • Find the number of elements in the jQuery object.
  • Set one or more CSS properties for the matched element
发布评论

评论列表(0)

  1. 暂无评论