$("div").addClass(function(index,className)?
? ? ? ? $("div").addClass(function(index,className) {
? ? ? ? ? ? //找到類名中包含了imooc的元素
? ? ? ? ? ? if(-1 !== className.indexOf('imooc')){
? ? ? ? ? ? ? ? //this指向匹配元素集合中的當(dāng)前元素
? ? ? ? ? ? ? ? $(this).addClass('imoocClass')
? ? ? ? ? ? }
? ? ? ? });
function(index,className)中的index和className有什么作用,函數(shù)中并沒有使用這個參數(shù),
if(-1 !== className.indexOf('imooc'))是什么意思?麻煩細(xì)細(xì)解釋
2016-10-23
$("div").addClass(function(index,className) {
? ? ? ? ? ? //找到類名中包含了imooc的元素
? ? ? ? ? ? if(0=== className.indexOf('imooc') or 1=== className.indexOf('imooc')){
? ? ? ? ? ? ? ? //this指向匹配元素集合中的當(dāng)前元素
? ? ? ? ? ? ? ? $(this).addClass('imoocClass')
? ? ? ? ? ? }
? ? ? ? });
2016-07-22
首先你可以打印一下這個className的值,看下到底是什么。index的意思是某個具有className的div在document.getElementsByTagName中的索引,這里沒用到。-1 !== className.indexOf('imooc')的意思就是判斷imooc這個className存在,則執(zhí)行{}中的語句。
2016-07-20
indexOf方法獲取到指定的字符在字符串中第一次出現(xiàn)的位置,從0開始。如果一開始就是e則返回0,如果第二個位置是e則返回1,如果搜完整個字符串都沒有e則返回-1 。
反證法 如果該方法不返回-1 就證明該字符串中含有e字符。