国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

javascript - How to modify the class of an element after setting it using bootstrap?
phpcn_u1582
phpcn_u1582 2017-05-18 10:47:55
0
5
826

I set the class of an element, and now I want to change the class of the element through JavaScript. I tried jQuery's attr to modify the class attribute, but it didn't work.
html:

<nav>
    <ul class="pager" id="pageCtr">
        <li class="previous" id="prevBTN">
            <a href="#" onclick="prevPage()"><span>&larr;</span>前一頁</a>
        </li>
    </ul>
</nav>

js:

$('#prevBTN').className='previous disabled';
phpcn_u1582
phpcn_u1582

reply all(5)
某草草

Hypothetical elements

<p class="className" id="idValue"></p>

You can change it like this

var e = document.getElementById('idValue')
if (e) {
    e.className = "changedClassName"
}

Tried and tested.

jQuery just document.getElementById('idValue')改成$('#idValue') and it’s ready

黃舟

HTML:

<p id="id_test" class="old-bootstrap">

JS:

$('#id_test').className='new_class';

not passattr,而是通過className

PS:
If you want to add the attribute disabled to li, you can do this, instead of adding class but attrli添加屬性disabled可以這樣做,不是添加class而是attr
$('#prevBTN').attr('disabled', true);$('#prevBTN').attr('disabled', true);

Remove attr:

Option 1: The a tag does not support the disabled attribute, so you can just replace the a tag with the button tag: http://www.w3school.com.cn/ti...
Option 2: Use The a tag can also be used. By removing its href attribute, you can achieve a non-clickable effect, that is, $('#prevBTN').removeAttr('href');
Reference: You take the following code and run it on this

<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js"></script>
<script>
function test() {
    $('#id_test').removeAttr('href');
}

</script>
</head>

<body>
<nav>
    <ul class="pager" id="pageCtr">
        <li class="previous" id="prevBTN">
            <a href="#" id="id_test" onclick="prevPage()"><span>&larr;</span>前一頁</a>
        </li>
    </ul>
</nav>
<button id="id_test2" onClick="test()">disable</button>
</body>
</html>
劉奇

What is the code of the original poster?
Why is it okay for me to directly use attr() to set the class?

In addition, jQuery also has two methods: addClass() and removeClass. I generally use these two methods to operate classes.

黃舟

jQuery has special methods for operating classes, you can check the relevant API

淡淡煙草味

Thanks to @daryl and @tony_yin for their enthusiastic answers. I want to use it myself <li> 標(biāo)簽里面的 <a> 換成 <button> ,能實現(xiàn)禁用按鈕效果,但是 .pager 的 BootStrap 默認(rèn)樣式會變化;
<li class="previous" id='prevBTN'> 換成 <li class="previous disabled" id='prevBTN'> 使用 document.getElementById('prevBTN').className='previous disabled'; 確實可行,但使用 jQuery 時會出現(xiàn)問題,$('prevBTN').className='previous disabled'; 就不能實現(xiàn),主要原因是 $('prevBTN') 并不能得到 DOM 元素,而 className 是 DOM 里面的屬性,$(selector) I can only get a collection of DOM elements that meet the selector conditions,

jQuery() (or $()) with an id selector as its argument will return a jQuery object containing a collection of either zero or one DOM element.

Solution:$('prevBTN').get(0).className='previous disabled';

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template