javascript - IE9 this = DispHTMLWindow2 在原型(prototype)函数中

标签 javascript internet-explorer-9

我在我的通用 js 中创建了一个名为 startsWith 的扩展函数。

它是这样的:

String.prototype.startsWith = function (str) {
 if (this.indexOf(str) == 0) {
     return true;
 }
 return false;
}

在 IE9 中运行时,我收到一条错误消息:“对象不支持属性或方法‘indexOf’”。

在调试器中查看时,这似乎是 DispHTMLWindow2。

有什么帮助吗?

谢谢。

最佳答案

您可以尝试以下摘自 this site 的内容

String.prototype.startsWith = function(str) 
{return (this.match("^"+str)==str)}

String.prototype.endsWith = function(str) 
{return (this.match(str+"$")==str)}

String.prototype.trim = function(){return 
(this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))}

关于javascript - IE9 this = DispHTMLWindow2 在原型(prototype)函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6691618/

相关文章:

javascript - 检测访问者是否滚动到页面中的某个点的最明智的方法是什么?

c# - 通过 jquery 在中继器内单击复选框

javascript - IE9 selectSingleNode 在 beta 中丢失,如何在 JavaScript 中克服这个问题?

html - IE9 及更新版本中菜单中的白色 block

printing - 绕过 IE9 中的 Printdialog

javascript - 创建 3 个可滚动网格

javascript - 如何使用手写笔在 HTML5 Canvas 上绘图

javascript - Quilljs setContents(json_string_with_escapes)

用于由特定字符串包围的数字和空格的 JavaScript 正则表达式

html - DXIMageTransform.Microsoft.Matrix 在 IE9 中模糊