求問(wèn)if(-1 !== className.indexOf('imooc'))怎么理解?
<script type="text/javascript">?
? ??
? ? ? ? //通過(guò)className(fucntion)方法
? ? ? ? //這個(gè)函數(shù)返回一個(gè)或更多用空格隔開(kāi)的要增加的樣式名。
? ? ? ? //接收index 參數(shù)表示元素在匹配集合中的索引位置和html 參數(shù)表示元素上原來(lái)的 HTML 內(nèi)容
? ? ? ? //找到所有的div,然后通過(guò)addClass設(shè)置顏色,根據(jù)返回的className的判斷,
? ? ? ? $("div").addClass(function(index,className) {
? ? ? ? ? ? //找到類名中包含了imooc的元素
? ? ? ? ? ? if(-1 !== className.indexOf('imooc')){
? ? ? ? ? ? ? ? //this指向匹配元素集合中的當(dāng)前元素
? ? ? ? ? ? ? ? $(this).addClass('imoocClass')
? ? ? ? ? ? }
? ? ? ? });
? ? </script>
請(qǐng)問(wèn)這里的“?if(-1 !== className.indexOf('imooc'))”怎么理解?
2016-07-11
indexOf是返回()里字符串出現(xiàn)的位置,從0開(kāi)始,如果沒(méi)有則返回-1。
所以就是當(dāng)className里面有imooc時(shí)。
2017-05-31
收到了 ,謝謝
2017-02-23
懂是懂了,但感覺(jué)這句話還是有一定難度的,我自己不會(huì)用
2016-07-21
長(zhǎng)知識(shí)了?