项目中遇到的bug(web前端-持续更新)

版权声明:本文为吴孔云博客原创文章,转载请注明出处并带上链接,谢谢。 https://blog.csdn.net/wkyseo/article/details/51159370

项目中遇到的常见bug,及时整理。

input放在a标签里面单击不能获取input的光标(IE环境下)

双击才可以获得焦点,目前有的解决方案:

  • 不要给a标签添加href属性;

  • 不要在外面套上a标签。

隐藏input标签的光标

项目需求:input值json加载,只读+光标隐藏,通用的解决方案有其他标签模拟,但是不能改input
所以解决方案为给input加下面这两个属性:

//只读 
readonly="readonly" 
//隐藏光标
unselectable="on"

返回私有数组

返回数组的一个副本,这样改动就不会影响原数组,只是副本而已

    var array = (function () {
       var days = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'];
        return {
            getDays: function () {
                return days.slice();
            }
        }
    })()

jquery选择器的扩展

//jQuery contains 选择器,对Contains查找的内容不区分大小写
jQuery.expr[':'].Contains = function (a, i, m) {
    return jQuery(a).text().toUpperCase()
        .indexOf(m[3].toUpperCase()) >= 0;
};

例子

<div>john</div>
<div>John</div>
<div>hey hey JOHN hey hey</div>
$('div:Contains("john")') //会选择到两个div

当一个变量被声明后,扩充其属性并不会改变原数据类型

var a = 'foo'; 
a[1] = 'O'; 
console.log(0.1+0.2==0.3||a); //'foo'

闭包是函数的嵌套定义,而不是函数的嵌套调用

function foo(){
    console.log(a);
}
function bar () {
    var a = 3; 
    foo(); 
}
var a = 2;
bar(); //2

DOMContentLoaded兼容IE9以下版本

//jQuery的实现

      // Mozilla, Opera and webkit nightlies currently support this event
      if ( document.addEventListener ) {
          // Use the handy event callback
          document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );

          // A fallback to window.onload, that will always work
          window.addEventListener( "load", jQuery.ready, false );

          // If IE event model is used
      } else if ( document.attachEvent ) {
          // ensure firing before onload,
          // maybe late but safe also for iframes
          document.attachEvent( "onreadystatechange", DOMContentLoaded );

          // A fallback to window.onload, that will always work
          window.attachEvent( "onload", jQuery.ready );

          // If IE and not a frame
          // continually check to see if the document is ready
          var toplevel = false;

          try {
              toplevel = window.frameElement == null;
          } catch(e) {}

          if ( document.documentElement.doScroll && toplevel ) {
              doScrollCheck();
          }
      }

        //函数DOMContentLoaded的赋值
        if ( document.addEventListener ) {
            DOMContentLoaded = function() {
                document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
                jQuery.ready();
            };

        } else if ( document.attachEvent ) {
            DOMContentLoaded = function() {
                // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
                if ( document.readyState === "complete" ) {
                    document.detachEvent( "onreadystatechange", DOMContentLoaded );
                    jQuery.ready();
                }
            };
        }

// The DOM ready check for Internet Explorer
        function doScrollCheck() {
            if ( jQuery.isReady ) {
                return;
            }

            try {
                // If IE is used, use the trick by Diego Perini
                // http://javascript.nwbox.com/IEContentLoaded/
                document.documentElement.doScroll("left");
            } catch(e) {
                setTimeout( doScrollCheck, 1 );
                return;
            }

            // and execute any waiting functions
            jQuery.ready();
        }

//网友的实现
    // @win window reference
    // @fn function reference
    function contentLoaded(win, fn) {

        var done = false, top = true,

                doc = win.document,
                root = doc.documentElement,
                modern = doc.addEventListener,

                add = modern ? 'addEventListener' : 'attachEvent',
                rem = modern ? 'removeEventListener' : 'detachEvent',
                pre = modern ? '' : 'on',

                init = function(e) {
                    if (e.type == 'readystatechange' && doc.readyState != 'complete') {
                        return;
                    }

                    //load事件在win上,移除事件监听(readystatechange, DOMContentLoaded, load)
                    (e.type == 'load' ? win : doc)[rem](pre + e.type, init, false);

                    //保证fn只执行一次
                    if (!done && (done = true)) fn.call(win, e || e.type);
                },

                poll = function() {
                    try { root.doScroll('left'); } catch(e) { setTimeout(poll, 50); return; }
                    init('poll');
                };

        if (doc.readyState == 'complete') fn.call(win, 'lazy');
        else {
            if (!modern && root.doScroll) {
                try { top = !win.frameElement; } catch(e) { }
                if (top) poll();
            }
            doc[add](pre + 'DOMContentLoaded', init, false); //触发时,doc.readyState为'interactive'
            doc[add](pre + 'readystatechange', init, false); //会触发两次,readystatechange先触发,再触发DOMContentLoaded, 最后才是load
            win[add](pre + 'load', init, false);
        }

    }

document.querySelectorAll和getElementsByTagName的区别

//html代码
<ul>
    <li>a</li>
    <li>b</li>
    <li>c</li>
</ul>

//script代码
    var ul = document.querySelector('ul');
    var li = ul.querySelectorAll('li'),
            tagLi = ul.getElementsByTagName('li'); //动态取值 
    ul.appendChild(document.createElement('li'));
    console.log(li.length); //3   li.toString()为[object NodeList]
    console.log(tagLi.length); //4 tagLi.toString()为[object HTMLCollection]
展开阅读全文

没有更多推荐了,返回首页