Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Console.Log()而不是jQuery选择对象中显示HTML_Javascript_Jquery_Google Chrome_Developer Tools - Fatal编程技术网

Javascript 在Console.Log()而不是jQuery选择对象中显示HTML

Javascript 在Console.Log()而不是jQuery选择对象中显示HTML,javascript,jquery,google-chrome,developer-tools,Javascript,Jquery,Google Chrome,Developer Tools,我将在Chrome开发者控制台中输出真正的html,以便于调试。 所以我想做一个chrome扩展,就是 我将真正的console.log()复制到console.nativeLog();我在console.log()中添加了自己的自定义函数 代码如下: <div class="myDiv"> <input type="text" id="inp1" title="title1" /> <input type="text" id

我将在Chrome开发者控制台中输出真正的html,以便于调试。 所以我想做一个chrome扩展,就是

我将真正的console.log()复制到console.nativeLog();我在console.log()中添加了自己的自定义函数

代码如下:

    <div class="myDiv">
        <input type="text" id="inp1" title="title1" />
        <input type="text" id="inp2" title="title2" />
        <input type="text" id="inp3" title="title3" />
        <input type="text" id="inp4"  />
        <input type="text" id="test" value="">
    </div>
    <input type="button" id="btn1" value="Add" />
    <script type="text/javascript">
        console.nativeLog = console.log;
    var arr= new Array();
    for(var i=0;i<100;i++){
        arr[i] = i+','+i;
    }
    var fav = JSON.parse('[{"href":"/EMS-ILS/Modules/Supplier_Profile/Supplier_Profile.aspx?ModID=6&WebPageID=38","text":"Supplier Profile"},{"href":"/EMS-ILS/Modules/Customer_Profile/Customer_Profile.aspx?ModID=6&WebPageID=57","text":"Customer Profile"},{"href":"/EMS-ILS/Modules/Costing_Profile/Costing_Profile.aspx?ModID=6&WebPageID=50","text":"Costing Profile"}]')
        console.log = function (val){
            if(typeof(val)=='string'){
                console.nativeLog(val);
                return;
            }
            try{
                for(var x=0;x<arguments.length;x++){
                    var arr = arguments[x];
                    try{
                        if(!arr.length)
                            console.nativeLog(arr);
                        else {
                            for(var i=0;i<arr.length;i++)
                                console.nativeLog(arr[i]);
                        }
                    }catch(err1){
                        console.nativeLog(arr);
                    }
                }
            }   
            catch(err2){
                console.nativeLog(val);
            }
        }           
        $(document).ready(function(){
            console.log('-------------');
            console.log($('input'));
            console.log('-------------');
            console.log($('#inp1'));
            console.log('-------------');
            console.log($('#badId'));
            console.log('-------------');   
                            console.log($('input'), $('#bad'), $('input:text'),  fav, 0, arr)


        });
    </script>

console.nativeLog=console.log;
var arr=新数组();
对于(var i=0;i试试看

    console.log($('#badId')[0] != undefined ? $('#badId') : 'do not exist');
试试看

.html()

此外,您还可以检查此属性的
jquery.length
属性:

var arr = arguments[x];
                    try{
                        if(!arr.length)

只需在打印jQuery对象之前添加判断

console.log = function (val){
    if(typeof(val)=='string'){
        console.nativeLog(val);
        return;
    }
    else if(val instanceof jQuery && val.length==0)
    {
        console.nativeLog("A jQuery object with no html element");
        return;
    }
检查此提琴。如果jQuery对象为空,这将在单独的一行上打印每个参数并打印[]:

console.nativeLog = console.log;
console.log = function(val) {
    var x = 0;
    for (x; x < arguments.length; x++) {
        var item = arguments[x];
        // check if we are dealing with jQuery object
        if (item instanceof jQuery) {
            // jQuery objects with length property are
            // the only ones we want to print
            if (item.length) {
                for (var i = 0; i < item.length; i++) {
                    console.nativeLog(item[i]);
                }
            } else {
                console.nativeLog('[]');
            }
        } else {
            console.nativeLog(item);
        }
    }
}

我想如果($('#badId').length>0)我想在console.log函数中检查这一点会更好:(他覆盖了console.log实现。他希望在他的实现中进行修复。看看代码。您希望输出是什么?空行还是根本没有输出?可能是空数组[]下面的实现只生成一个空行。这使逻辑更简单,但我可以修改它以打印空数组。好的,我将实现更新为打印[]当jquery对象为空时。您可以将其更改为最有效的。这将适用于jquery对象,但会破坏其他类型的本机console.log实现。例如,长度为0的数组将记录为“一个jquery对象…”@teddybeard你是对的。应该先判断它是jquery对象。我的答案已更新。谢谢你的时间。我尝试了你的代码。它会全部打印。但问题是它会一起打印,使阅读变得非常困难。我将更新我的问题第一个版本不会一起打印:
console.nativeLog = console.log;
console.log = function(val) {
    var x = 0;
    for (x; x < arguments.length; x++) {
        var item = arguments[x];
        // check if we are dealing with jQuery object
        if (item instanceof jQuery) {
            // jQuery objects with length property are
            // the only ones we want to print
            if (item.length) {
                for (var i = 0; i < item.length; i++) {
                    console.nativeLog(item[i]);
                }
            } else {
                console.nativeLog('[]');
            }
        } else {
            console.nativeLog(item);
        }
    }
}
console.nativeLog = console.log;
console.log = function() {
    var x = 0;
    var output = [];
    for (x; x < arguments.length; x++) {
        item = arguments[x];
        if (item instanceof jQuery) {
            if (item.length) {
                for (var i = 0; i < item.length; i++) {
                    output.push(item[i]);
                }
            } else {
                output.push('[]');
            }
        } else {
            output.push(item);
        }
    }
    console.nativeLog.apply(this, output);
}