获取错误"不能调用方法'替换'undefined"

Get error "Cannot call method 'replace' of undefined"

本文关键字:quot undefined 替换 方法 不能 取错误 获取 调用      更新时间:2023-09-26

我尝试了以下代码:

var contentTag = $(this.options.id);   //Taking id from user function in plugin function.
var strContent = contentTag.innertHTML.replace('id="paginationTable"','style="display:none";');

将dom元素放在引号中,正如注释中指出的那样,inner拼写错误,并且也不是jquery属性,它是dom元素的属性。

你可以使用jquery属性html()来代替检索html属性,或者如果你想获得dom元素,你可以使用括号[0]或调用jquery方法get()

$('#parentID').html().replace('...');

$('#parentID').get().innerHTML.replace('...');

如果你想简单地显示一个元素,或隐藏一个元素,你可以使用jquery show()hide()函数

//show the element
$('#idOfDOMElementToShow').show();
//hide the element
$('#idOfDOMElementToHide').hide();

如果你想改变属性或类,看看css(), prop()或attr()

我相信你想隐藏paginationTable。

在jQuery中应该是

$("#paginationTable").hide();

String操作replace的工作原理是替换字符串或正则表达式并返回更改后的字符串

var string = someString.replace("someotherstring","yetanotherstring") 

可以使用正则表达式作为第一个表达式,使用字符串或函数作为替换

innerHTML是DOM而不是jQuery,它使用。html(),正如其他人提到的

jQUery有一个可用于jQUery元素的replaceWith