js兼容性,chrome不兼容

slysmart 2018-07-06 09:11:38

function checkall() {
var chk = document.getElementById("tb").getElementsByTagName("input");
for (var i = 0; i < chk.length; i++) {
if (chk[i].type.toLowerCase() == "checkbox")
if (chk[i].parentNode.parentNode.childNodes[2].innerHTML.indexOf('使用') > -1) {
chk[i].checked = !chk[i].checked;
}
}
}

各位,以上语句在ie中兼容,chrome则无效,哪里写法有问题?TKS
...全文
183 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
slysmart 2018-07-06
  • 打赏
  • 举报
回复
引用 4 楼 CFW_CSDN 的回复:
[quote=引用 1 楼 slysmart 的回复:]
是Chrome不支持 indexOf ,有什么替代方法?

你是认真的吗?Chrome不支持 indexOf?
我猜是chk[i].parentNode.parentNode.childNodes[2].innerHTML取得值不对,
childNodes获取节点,不同浏览器表现不同,换成children试试。
[/quote]
换成children就正常了,非常感谢
slysmart 2018-07-06
  • 打赏
  • 举报
回复
引用 4 楼 CFW_CSDN 的回复:
[quote=引用 1 楼 slysmart 的回复:]
是Chrome不支持 indexOf ,有什么替代方法?

你是认真的吗?Chrome不支持 indexOf?
我猜是chk[i].parentNode.parentNode.childNodes[2].innerHTML取得值不对,
childNodes获取节点,不同浏览器表现不同,换成children试试。
[/quote]
IE8.0中正常,我用CHROME F12 中显示indexOf节点错误
slysmart 2018-07-06
  • 打赏
  • 举报
回复
引用 2 楼 qq_20203755 的回复:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo2</title>
</head>
<body>

</body>

<script>
// Production steps of ECMA-262, Edition 5, 15.4.4.14
// Reference: http://es5.github.io/#x15.4.4.14
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(searchElement, fromIndex) {

var k;

// 1. Let O be the result of calling ToObject passing
// the this value as the argument.
if (this == null) {
throw new TypeError('"this" is null or not defined');
}

var O = Object(this);

// 2. Let lenValue be the result of calling the Get
// internal method of O with the argument "length".
// 3. Let len be ToUint32(lenValue).
var len = O.length >>> 0;

// 4. If len is 0, return -1.
if (len === 0) {
return -1;
}

// 5. If argument fromIndex was passed let n be
// ToInteger(fromIndex); else let n be 0.
var n = +fromIndex || 0;

if (Math.abs(n) === Infinity) {
n = 0;
}

// 6. If n >= len, return -1.
if (n >= len) {
return -1;
}

// 7. If n >= 0, then Let k be n.
// 8. Else, n<0, Let k be len - abs(n).
// If k is less than 0, then let k be 0.
k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);

// 9. Repeat, while k < len
while (k < len) {
// a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator
// b. Let kPresent be the result of calling the
// HasProperty internal method of O with argument Pk.
// This step can be combined with c
// c. If kPresent is true, then
// i. Let elementK be the result of calling the Get
// internal method of O with the argument ToString(k).
// ii. Let same be the result of applying the
// Strict Equality Comparison Algorithm to
// searchElement and elementK.
// iii. If same is true, return k.
if (k in O && O[k] === searchElement) {
return k;
}
k++;
}
return -1;
};
}
</script>

<script>
var a = [2, 9, 9];
alert(a.indexOf(9));
</script>

</html>

这个不行,在我的代码中还是无效
昵称已存在吗 2018-07-06
  • 打赏
  • 举报
回复
引用 1 楼 slysmart 的回复:
是Chrome不支持 indexOf ,有什么替代方法?

你是认真的吗?Chrome不支持 indexOf?
我猜是chk[i].parentNode.parentNode.childNodes[2].innerHTML取得值不对,
childNodes获取节点,不同浏览器表现不同,换成children试试。
伟洪winni 2018-07-06
  • 打赏
  • 举报
回复
伟洪winni 2018-07-06
  • 打赏
  • 举报
回复

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo2</title>
</head>
<body>

</body>

<script>
// Production steps of ECMA-262, Edition 5, 15.4.4.14
// Reference: http://es5.github.io/#x15.4.4.14
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(searchElement, fromIndex) {

var k;

// 1. Let O be the result of calling ToObject passing
// the this value as the argument.
if (this == null) {
throw new TypeError('"this" is null or not defined');
}

var O = Object(this);

// 2. Let lenValue be the result of calling the Get
// internal method of O with the argument "length".
// 3. Let len be ToUint32(lenValue).
var len = O.length >>> 0;

// 4. If len is 0, return -1.
if (len === 0) {
return -1;
}

// 5. If argument fromIndex was passed let n be
// ToInteger(fromIndex); else let n be 0.
var n = +fromIndex || 0;

if (Math.abs(n) === Infinity) {
n = 0;
}

// 6. If n >= len, return -1.
if (n >= len) {
return -1;
}

// 7. If n >= 0, then Let k be n.
// 8. Else, n<0, Let k be len - abs(n).
// If k is less than 0, then let k be 0.
k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);

// 9. Repeat, while k < len
while (k < len) {
// a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator
// b. Let kPresent be the result of calling the
// HasProperty internal method of O with argument Pk.
// This step can be combined with c
// c. If kPresent is true, then
// i. Let elementK be the result of calling the Get
// internal method of O with the argument ToString(k).
// ii. Let same be the result of applying the
// Strict Equality Comparison Algorithm to
// searchElement and elementK.
// iii. If same is true, return k.
if (k in O && O[k] === searchElement) {
return k;
}
k++;
}
return -1;
};
}
</script>

<script>
var a = [2, 9, 9];
alert(a.indexOf(9));
</script>

</html>
slysmart 2018-07-06
  • 打赏
  • 举报
回复
是Chrome不支持 indexOf ,有什么替代方法?

87,955

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧