GVKun编程网logo

javascript – 强迫ie8中的dom rerender

13

如果您对javascript–强迫ie8中的domrerender感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解javascript–强迫ie8中的domrerender的各种细节,此外还有关

如果您对javascript – 强迫ie8中的dom rerender感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解javascript – 强迫ie8中的dom rerender的各种细节,此外还有关于IE7 / IE8中的Javascript JSON日期解析返回NaN、IE8中javascript trim方法失效、IE8中使用javascript动态加载CSS的解决方法_javascript技巧、javascript document.referrer 用法_javascript技巧的实用技巧。

本文目录一览:

javascript – 强迫ie8中的dom rerender

javascript – 强迫ie8中的dom rerender

我已经尝试了所有经典的技巧,让我的网页重新呈现在dom中所做的更改但没有任何效果.我已经试过了

element.className = element.className;

以及访问我改变的一部分dom,但这也不起作用.

这是我的代码. Onload正文调用queryDB,在html中有一个id为“plants”的段落.到目前为止,此代码仅适用于firefox 3和chrome.

var timeout = 5000; //get new plants every 5 seconds

function queryDB() {
    responseDoc = getXMLFrom("ajax/getallplants.PHP");
    paragraph = document.getElementById("plants");
    table = document.createElement("table");
    table.setAttribute("class","viewTable");

    //clean up the last query
    cleanUpLastResult(paragraph);

    //loop through the responseDoc and dynamically add plants
    if(plantsFound(responseDoc)) {
        for(i = 0; i < responseDoc.documentElement.childNodes.length; i++) {
            currentChild = responseDoc.documentElement.childNodes[i];

            row = document.createElement("tr");
            //old way of printing where the whole sci name and common name was just text
            /*paragraph.appendChild(document.createTextNode(responseDoc.documentElement.childNodes[i].firstChild.nodeValue));
            paragraph.appendChild(document.createElement("br"));*/

            //newer way of printing where the common name is bolded
            /*paragraph.appendChild(document.createTextNode(currentChild.firstChild.nodeValue + " "));
            commonName = document.createElement("b");
            commonName.appendChild(document.createTextNode(currentChild.getAttribute("commonname")));
            paragraph.appendChild(commonName);
            paragraph.appendChild(document.createElement("br"));*/

            //newest way of printing that prints to a table
            col1 = document.createElement("td");
            col1.setAttribute("class","viewTable");
            col1.appendChild(document.createTextNode(currentChild.firstChild.nodeValue));

            col2 = document.createElement("td");
            col2.setAttribute("class","viewTable");
            col2Bold = document.createElement("b");
            col2Bold.appendChild(document.createTextNode(currentChild.getAttribute("commonname")));
            col2.appendChild(col2Bold);

            row.appendChild(col1);
            row.appendChild(col2);

            table.appendChild(row);
        }

        paragraph.appendChild(table);

        paragraph.className = paragraph.className;
        paragraph.firstChild.className = paragraph.firstChild.className;
    }
    else {
        paragraph.appendChild(document.createTextNode("no plants currently entered"));
    }

    //re-add the callback
    setTimeout(queryDB,timeout);
}

function plantsFound(responseDoc) {
    if(responseDoc.documentElement == null) {
        return false;
    }
    else {
        if(responseDoc.documentElement.firstChild.nodeType == 3) {
            //text node so no children

            return false;
        }
        else {
            return true;
        }
    }
}

function cleanUpLastResult(paragraph) {
    //old way of cleaning up where everything was only a childnode of the paragraph
    /*while(paragraph.childNodes.length >= 1) {
        paragraph.removeChild(paragraph.firstChild);
    }*/

    /* The three possible cases:
     * 1 first execution time so paragraph has no child
     * 2 nth execution time but nothing was found in db so only a textnode
     * 3 nth execution and there's a whole table to clean up
     */
    if(paragraph.firstChild == null) {
        //nothing there so nothing to delete
    }
    else if(paragraph.firstChild.nodeValue != null) {
        //no table printed,just remove that text node

        paragraph.removeChild(paragraph.firstChild);
    }
    else {
        //delete the whole table

        table = paragraph.firstChild;

        //remove each row
        while(table.childNodes.length >= 1) {
            //remove the two columns in it and their stuff

            row = table.firstChild;

            col1 = row.firstChild;
            col2 = row.lastChild;

            //remove column1 and it's text node
            col1.removeChild(col1.firstChild);
            row.removeChild(row.firstChild);

            //remove column2,it's bold node and its text node
            col2.firstChild.removeChild(col2.firstChild.firstChild);
            col2.removeChild(col2.firstChild);
            row.removeChild(row.firstChild);

            table.removeChild(row);
        }

        //finally delete the table
        paragraph.removeChild(paragraph.firstChild);
    }
}

function getXMLFrom(url) {
    if(window.XMLHttpRequest) {
        //regular browser
        xmlhttp = new XMLHttpRequest();
    }
    else {
        //ie6
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET",url,false);
    xmlhttp.send();
    responseDoc = xmlhttp.responseDoc;

    if(responseDoc == null) {
        if(window.XMLHttpRequest && (typeof DOMParser != "undefined")) {
            //firefox

            var parser = new DOMParser();
            responseDoc = parser.parseFromString(xmlhttp.responseText,"text/xml");
        }
        else {
            //ie6 or ie7

            var doc = new ActiveXObject("Microsoft.XMLDOM");
            doc.async = true;
            doc.loadXML(xmlhttp.responseText);
            responseDoc = doc;
        }

        if(responseDoc == null) {
            alert("error in parser xml from: " + url);
        }

        return responseDoc;
    }
}

我还测试了responseDoc,我知道我从getallplants.PHP得到了正确的响应,它生成了各种植物的xml表示.有想法该怎么解决这个吗?此外,由于各种原因,我不能使用JQuery.

编辑我有一个准好的解决方案,我在另一个SO线程上找到.如果我添加document.write(document.all [0] .innerHTML);在我将表格设置为段落的子表后查询DB.唯一的问题是,如果我这样做,页面将不会每5分钟刷新一次.因此,如果它改变了数据库,那么必须刷新这个页面,这使得javascript异步从数据库中获取信息的目的失败了.

解决方法

几个星期前,我遇到了类似的问题,并且在body元素中添加了一个类.之后可以删除添加的类.

IE7 / IE8中的Javascript JSON日期解析返回NaN

IE7 / IE8中的Javascript JSON日期解析返回NaN

我正在解析JSON事件供稿中的日期-但日期在IE7 / 8中显示为“ NaN”:

// Variable from JSON feed (using JQuery's $.getJSON)
var start_time = '2012-06-24T17:00:00-07:00';

// How I'm currently extracting the Month & Day
var d = new Date(start_time);
var month = d.getMonth();
var day = d.getDate();

document.write(month+'/'+day);// "6/24" in most browsers,"Nan/Nan" in IE7/8

我究竟做错了什么?谢谢!

IE8中javascript trim方法失效

IE8中javascript trim方法失效

这是一个很纠结的问题,之前一直没在意,后来偶然一次发现写的代码在IE8下竟然报错!并且报错的是trim方法,百思不得其解,后来发现原来我用的是javascript中原生的trim方法,但是IE8下的确没有这个方法。

解决办法:

                使用jquery的$.trim()方法搞定!

例:

        var vistValues = $("#otherDiv #hidDiv input").val();    //需要回访的信息

var vistValue = $.trim(vistValues);        //IE8不支持trim方法。


$("#otherDiv #hidDiv input").val().trim();//此语句IE8不支持

IE8中使用javascript动态加载CSS的解决方法_javascript技巧

IE8中使用javascript动态加载CSS的解决方法_javascript技巧

众所周知做前端开发的都恨不得踹IE开发者几脚,IE开发者名声之差不低于GFW开发者,昧着良心搞坏市场,人人得而诛之,但是在中国这些地方市场占有率摆在那里,没办法只能向现实低头。

最近我们产品需要在浏览器里动态载入一段CSS,以前的代码是直接用的:

复制代码 代码如下:

var bubbleCss = document.createElement(''style'');
bubbleCss.type = ''text/css'';
bubbleCss.innerHTML = blc_conf.bubbleStyle;
document.getElementsByTagName(''head'')[0].appendChild(bubbleCss);

不过这个只有IE9支持,在IE8下会出问题,一直也没注意到这块,直到最近重构后做完整测试的时候才发现。
网上搜到一个技巧,试过,可行,但是有一些问题
复制代码 代码如下:

window.bc_bubble_css = blc_conf.bubbleStyle;
document.createStyleSheet("javascript:bc_bubble_css");

这里可以创建由变量bc_bubble_css定义的样式,不过由于HTML5逐渐普及,我们的css里也混入了一些css3 selector,使用这个方法会导致IE8的parser解析到css3 selector的时候抛异常并停止解析后续css,这让css只加载了一半,网上搜到的办法都是用StyleSheet类型的addRule来增加,不过这个需要自己指定css2 selector以及样式,
因此需要从CSS中拆开单个的规则,然后依次调用addRule,例子:
复制代码 代码如下:

var s = document.createStyleSheet();
var rules = blc_conf.bubbleStyle.replace(/\/\*[^\*]*\*\//g, "").replace(/@[^{]*\{/g, '''').match(/[^\{\}]+\{[^\}]+\}/g);
for(var i = 0; i     var m = rules[i].match(/(.*)\s*\{\s*(.*)\}/);
    if(m) {
        try {
            s.addRule(m[1], m[2]);
        } catch(e) {
        }
    }
}

开头有两个替换,分别去掉注视和部分css3 的selector,不过依然有漏网的selector,需要在后面try catch 捉一下。

另外再次鄙视设计IE接口的人

javascript document.referrer 用法_javascript技巧

javascript document.referrer 用法_javascript技巧

举例:
1. a.html文件内容如下:
浏览b.html
2. b.html文件中的内容如下:





3. 则在通过a.html中的超链接访问b.html的时候,显示的结果是:
http://127.0.0.1:8180/a.html

说明:

经过测试,需要将两个文件放在服务器中才能得到想要的结果,若直接在本地文件夹中则得到空字符串,若直接在浏览器地址栏中输入b.html的URL地址或使用打开菜单访问b.html,则document.referrer的值为空字符串。

今天的关于javascript – 强迫ie8中的dom rerender的分享已经结束,谢谢您的关注,如果想了解更多关于IE7 / IE8中的Javascript JSON日期解析返回NaN、IE8中javascript trim方法失效、IE8中使用javascript动态加载CSS的解决方法_javascript技巧、javascript document.referrer 用法_javascript技巧的相关知识,请在本站进行查询。

本文标签:

上一篇javascript – js中的三个或更多root(javascript三个部分)

下一篇javascript替换2个字符(js替换多个字符)