Показать сообщение отдельно
  #9 (permalink)  
Старый 20.09.2011, 17:06
sinistral
Посмотреть профиль Найти все сообщения от melky
 
Регистрация: 28.03.2011
Сообщений: 5,418

я тоже изменю его


// коллекцию не придумал,как заменить.
var all=document.querySelectorAll?document.querySelectorAll("[id]"):document.getElementsByTagName("*");

function getElementByIdWithoutCase( id ) {

        if( document.getElementById(id) ) return document.getElementById(id);

        var idReg = new RegExp( id, "i" ), i=0;
     
        /*
         пока не пройдем все элементы
         или не найдём интересующий
        */
        while( all[i] && !idReg.test( all[i].id) ) i+=1;

        // т.к. цикл истанавливается на первом совпадении,
        // то тут будет интересующий нас элемент
        return all[i];
}


таким образом можно уменьшить количество кода и уйти от использования функции loop.

ЗЫ использовал присваивание заместо пост-инкремента, потому что присваивание быстрее на муравьиную долю. мелочь, а приятно
<body>
  <div id='idlowercase'></div>
  <span id='IDUPPERCASE'></span>
  <p id='idMixedCase'></p>
</body>

<script type="text/javascript">

var all=document.querySelectorAll?document.querySelectorAll("[id]"):document.getElementsByTagName("*");

function getElementByIdWithoutCase( id ) {
 
        if( document.getElementById(id) ) return document.getElementById(id);

        var idReg = new RegExp( id, "i" ),i=0;
    
        while( all[i] && !idReg.test( all[i].id) ) i+=1;

        return all[i];
}



// func end. it's test.
var res = [],b;

res.push((b=getElementByIdWithoutCase('idlowercase')).tagName+"#"+b.id)
res.push((b=getElementByIdWithoutCase('IDUPPERCASE')).tagName+"#"+b.id)
res.push((b=getElementByIdWithoutCase('idMixedCase')).tagName+"#"+b.id)

alert( res.join('\n') )
</script>

Последний раз редактировалось melky, 20.09.2011 в 17:25.
Ответить с цитированием