Javascript代码无法在40.0或更低版本的firefox旧版本上运行。但在firefox 41.0或更高版本或其他浏览器中运行良好 [英] Javascript code does not working on firefox old version like 40.0 or below.but works good in firefox 41.0 or above or other browser

查看:61
本文介绍了Javascript代码无法在40.0或更低版本的firefox旧版本上运行。但在firefox 41.0或更高版本或其他浏览器中运行良好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码:



I use this code:

var divs = document.getElementsByClassName("well-sm");
		for (var i = 0; i < divs.length; i++) {
			var div = divs[i];
			var span = div.getElementsByTagName("span")[0];
			if (span) {
				var strText = span.innerText;
				if(stringValue.toUpperCase() == strText.toUpperCase())
					{
						
							alert("Already exists!");
							return false;
						
					}
			}
		}







但是这段代码可以在firefox 40.0中运行。但是工作在40.0以上版本。

它显示了这种类型的错误:




but this code canot work in firefox 40.0.but works above 40.0 version.
It show this type of error:

TypeError: strText is undefined

但是其他浏览器或Firefox 47.0它没有显示此类型错误。



我有什么尝试过:



i只需将span.innertext替换为span.innerHtml

but other browser or firefox 47.0 it do not show this type error.

What I have tried:

i just replace span.innertext to span.innerHtml

推荐答案

试试这个
span.textContent







因为jquery使用下面的代码从选择器元素中获取文本






Since jquery uses the below peice of code to get the text from selector elements

 function (elem) {
    debugger;
	var node,
		ret = "",
		i = 0,
		nodeType = elem.nodeType;

	if ( nodeType ) {
		if ( nodeType === 1 || nodeType === 9 || nodeType === 11 ) {
			// Use textContent for elements
			// innerText usage removed for consistency of new lines (see #11153)
			if ( typeof elem.textContent === "string" ) {
				return elem.textContent;
			} else {
				// Traverse its children
				for ( elem = elem.firstChild; elem; elem = elem.nextSibling ) {
					ret += getText( elem );
				}
			}
		} else if ( nodeType === 3 || nodeType === 4 ) {
			return elem.nodeValue;
		}
		// Do not include comment or processing instruction nodes
	} else {

		// If no nodeType, this is expected to be an array
		for ( ; (node = elem[i]); i++ ) {
			// Do not traverse comment nodes
			ret += getText( node );
		}
	}
	return ret;
};


这篇关于Javascript代码无法在40.0或更低版本的firefox旧版本上运行。但在firefox 41.0或更高版本或其他浏览器中运行良好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆