1

最好的问候,我想编写一个 javascript 代码来写入<P><br data-mce-bogus="1"></P>其中存在的 Paragraph

<html><head>
</head>
<body>
<table id="contentMessage_tbl" class="mceLayout" cellspacing="0" cellpadding="0" role="presentation" style="width: 100%; height: 260px;">
<tbody>
<td class="mceIframeContainer mceFirst mceLast">
<iframe id="contentMessage_ifr" frameborder="0" src="javascript:""" allowtransparency="true" title="Rich Text AreaPress ALT-F10 for toolbar. Press ALT-0 for help" style="width: 100%; height: 216px; display: block;">
<head xmlns="http://www.w3.org/1999/xhtml">
<meta content="IE=7" http-equiv="X-UA-Compatible">
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body id="tinymce" class="mceContentBody " contenteditable="true" spellcheck="false" dir="ltr" style="font-size: 12px; font-family: arial,helvetica,sans-serif;">
<p>
<br data-mce-bogus="1">
</p>
</body>
</html>
</iframe>
</td>
</tbody>
</table>
</body>
</html>

我试过这段代码

javascript:document.getElementById('contentMessage_ifr').contentDocument.body.innerHTML="it works";

我的代码有问题吗,它在w3schools javascript 编辑器上运行良好

提前致谢

4

2 回答 2

0

您的代码正在获取 iframe 的 ID。试着抓住段落。这是一个应该可以工作的 jsfiddle 示例:http: //jsfiddle.net/kakashi/QR9N8/

如果你想使用 jQuery,你想要的相关代码是:

$('p:has(br[data-mce-bogus=1])').append("<p>Hi There</p>");
于 2012-10-25T16:07:02.553 回答
0

也许您的 html 代码不是很干净,您缺少一些 html 标签,例如“html”和“tr”,试试这个 html:

<html>
<head>
</head>
<body>
<table id="contentMessage_tbl" class="mceLayout" cellspacing="0" cellpadding="0" role="presentation" style="width: 100%; height: 260px;">
<tbody><tr>
<td class="mceIframeContainer mceFirst mceLast">
<iframe id="contentMessage_ifr" frameborder="0" src="javascript:"""   allowtransparency="true" title="Rich Text AreaPress ALT-F10 for toolbar. Press ALT-0 for  help" style="width: 100%; height: 216px; display: block;">
<html>
<head xmlns="http://www.w3.org/1999/xhtml">
<meta content="IE=7" http-equiv="X-UA-Compatible">
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body id="tinymce" class="mceContentBody " contenteditable="true" spellcheck="false" dir="ltr" style="font-size: 12px; font-family: arial,helvetica,sans-serif;">
<p>
<br data-mce-bogus="1">
</p>
</body>
</html>
</iframe>
</td></tr>
</tbody>
</table>
</body>
</html>​
于 2012-10-25T16:17:13.663 回答