Javascript 未捕获的TypeError:无法读取未定义的属性“htmlSource”
请帮我解决我的问题。我的网站上有Sobi2。这一切都很完美,但我将浏览器从Firefox改为Chrome,Sobis的条目编辑Html源按钮出现问题。当我按下输入按钮时,在Chrome中编辑Html源打开带有复选框和文本{$lang\u theme\u code\u wordwrap}的空弹出窗口,仅此而已。使用开发者工具,我得到下一个错误:Javascript 未捕获的TypeError:无法读取未定义的属性“htmlSource”,javascript,javascript-events,Javascript,Javascript Events,请帮我解决我的问题。我的网站上有Sobi2。这一切都很完美,但我将浏览器从Firefox改为Chrome,Sobis的条目编辑Html源按钮出现问题。当我按下输入按钮时,在Chrome中编辑Html源打开带有复选框和文本{$lang\u theme\u code\u wordwrap}的空弹出窗口,仅此而已。使用开发者工具,我得到下一个错误: Uncaught TypeError: Cannot read property 'htmlSource' of undefined source_edi
Uncaught TypeError: Cannot read property 'htmlSource' of undefined source_editor.js:18
这里是sourse_editor.js,函数onLoadInit中的错误:
function saveContent() {
tinyMCE.setContent(document.getElementById('htmlSource').value);
tinyMCE.closeWindow(window);
}
// Fixes some charcode issues
function fixContent(html) {
/* html = html.replace(new RegExp('<(p|hr|table|tr|td|ol|ul|object|embed|li|blockquote)', 'gi'),'\n<$1');
html = html.replace(new RegExp('<\/(p|ol|ul|li|table|tr|td|blockquote|object)>', 'gi'),'</$1>\n');
html = tinyMCE.regexpReplace(html, '<br />','<br />\n','gi');
html = tinyMCE.regexpReplace(html, '\n\n','\n','gi');*/
return html;
}
function onLoadInit() {
tinyMCEPopup.resizeToInnerSize();
document.forms[0].htmlSource.value = fixContent(tinyMCE.getContent(tinyMCE.getWindowArg('editor_id')));
resizeInputs();
}
function setWrap(val) {
var s = document.forms[0].htmlSource;
s.wrap = val;
if (tinyMCE.isGecko) {
var v = s.value;
var n = s.cloneNode(false);
n.setAttribute("wrap", val);
s.parentNode.replaceChild(n, s);
n.value = v;
}
}
function toggleWordWrap(elm) {
if (elm.checked)
setWrap('soft');
else
setWrap('off');
}
var wHeight=0, wWidth=0, owHeight=0, owWidth=0;
function resizeInputs() {
if (!tinyMCE.isMSIE) {
wHeight = self.innerHeight-80;
wWidth = self.innerWidth-16;
} else {
wHeight = document.body.clientHeight - 80;
wWidth = document.body.clientWidth - 16;
}
document.forms[0].htmlSource.style.height = Math.abs(wHeight) + 'px';
document.forms[0].htmlSource.style.width = Math.abs(wWidth) + 'px';
}
function renderWordWrap() {
if (tinyMCE.isMSIE || tinyMCE.isGecko)
document.write('<input type="checkbox" name="wraped" id="wraped" onclick="toggleWordWrap(this);" class="wordWrapCode" /><label for="wraped">{$lang_theme_code_wordwrap}</label>');
}
以下是source_editor.htm:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>{$lang_theme_code_title}</title>
<script language="javascript" type="text/javascript"
src="../../tiny_mce_popup.js"></script>
<script language="javascript" type="text/javascript"
src="jscripts/source_editor.js"></script>
<base target="_self" />
</head>
<body onload="tinyMCEPopup.executeOnLoad('onLoadInit();');"
onresize="resizeInputs();" style="display: none">
<form name="source" onsubmit="saveContent();" action="#">
<div style="float: left" class="title">{$lang_theme_code_title}</div>
<div style="float: right"><script language="javascript"
type="text/javascript">renderWordWrap();</script></div>
<textarea name="htmlSource" id="htmlSource" rows="15" cols="100"
style="width: 100%; height: 100%; font-family: 'Courier New',Courier,mono; font-size: 12px"
dir="ltr" wrap="off"></textarea>
<div class="mceActionPanel">
<div style="float: left"><input type="button" name="insert"
value="{$lang_update}" onclick="saveContent();" id="insert" /></div>
<div style="float: right"><input type="button" name="cancel"
value="{$lang_cancel}" onclick="tinyMCEPopup.close();" id="cancel" />
</div>
</div>
</form>
</body>
</html>
如果您仅将textarea包装在表单元素中,则应该解决此问题:
<form>
<textarea name="htmlSource" id="htmlSource" rows="15" cols="100"
style="width: 100%; height: 100%; font-family: 'Courier New',Courier,mono; font-size: 12px"
dir="ltr" wrap="off"></textarea>
</form>
任何人都不能阅读。。。“未定义错误”表示找不到您试图查询的元素。在您的情况下,document.forms[0]。运行console.logdocument.forms-是否看到返回的表单?如果您确定表单存在,为什么不通过ID、jQuery选择器或其他方式选择它?没有返回表单。console.logdocument.forms-未定义如果这是您的问题-该表单不存在,或者至少在您使用的选择过程中找不到该表单。你确定表单在页面中吗?正如我所描述的,试着用不同的方式选择它。所有这些在IE和Firefox中都能很好地工作,只有在Chrome中。有什么不同的选择方式?我对javascript了解不多,jQueryWell如果表单有一个ID,您可以使用document.getElementById'ID_here'或者,如果您包括jQuery,您可以通过任何其他方式选择表单。同样,通过ID,例如$'ID_here'或其名称,$'[name=name_here]'等等。