(Bootstrap 3.1.1)popover on show reset我的addClass?

时间:2014-04-15 05:57:25

标签: javascript jquery twitter-bootstrap

<button id="search-district" type="button" class="btn btn-sm btn-success" data-toggle="popover" data-placement="bottom" title="<a href='#' class='pull-right popover-close' onclick='$(&quot;#search-district&quot;).popover(&quot;hide&quot;);'>&times;</a>" data-html="true" data-content="

<a id='id_district_100' href='#' onclick='ChangeDistrictSelection(&quot;100&quot;);'>District123</a>

"><span class="glyphicon glyphicon-plus"></span> Dsitricts <span id="district_bracket" style="display:none;">[<span id="count_districts_">0</span>]</span></button>

和JavaScript函数

function ChangeDistrictSelection(id)
{
$('#id_district_'+id).addClass("selected");
}

当我点击了District123时,我的JavaScript添加了Class&#34; active&#34; ...但是,之后当show action pop on show时,popover重置我的类:(

3 个答案:

答案 0 :(得分:0)

你选择的css是什么,你必须用!important标记它才能覆盖现有的css。

实施例

.selected {
  background-color:gray !important;
}

答案 1 :(得分:0)

你的问题不是很清楚。请解释一下,我们可以帮到你。

也请不要这样。

你使用onclick将'100'传递给函数,然后你试图通过id获取元素。但已经等于id了。

<强> WHY ??

你这里做错了。查看您的密码。

<a id='id_district_100' href='#' onclick='ChangeDistrictSelection(&quot;100&quot;);'>District123</a>

你的功能也看了

function ChangeDistrictSelection(id) {
    //id will be 100 as your code.
   //again you are trying to get the element by id. see your html <a id="id_district_100"> 
   //It's already equal to $('#id_district_'+100) - why do you passing that 100 and trying to get element by id??
   $('#id_district_'+id).addClass("selected");
}

如果要将该类添加到该元素,可以使用它。

function ChangeDistrictSelection(id){
   //alert(id);
   $(id).addClass('selected');
}

在那样的html传递中

<a id='id_district_100' href="#" onclick='ChangeDistrictSelection(id)'>District123</a>

答案 2 :(得分:0)

ok ... v2

HTML:

<button id="search-district" type="button" class="btn btn-sm btn-success" data-toggle="popover" data-placement="bottom" title="<a href='#' class='pull-right popover-close' onclick='$(&quot;#search-district&quot;).popover(&quot;hide&quot;);'>&times;</a>‌​" data-html="true" data-content=" <a id='id_district_100' href='#' onclick='ChangeDistrictSelectionCss(&quot;100&quot;);'>District123</a> ... ">

JS:

function ChangeDistrictSelectionCSS(id){
$('#id_district_'+id).css("color","red");
}

它的工作,但是当再次点击按钮(id =&#34; search-district&#34;)时,css颜色不是红色