简体   繁体   English

JavaScript:在 <p> 标签之间也插入一个新标签 <p> 标签(输出)

[英]JavaScript: transform a text between <p> tags into a new one also between <p> tags (output)

I'm trying to operate a transformation onLoad from a <p> text </p> in order to create a <p> new modified text </p> (the point is I don't wanna use textareas ) in simple javascript. 我正在尝试从<p>文本</ p>进行转换onLoad ,以便在简单的javascript中创建<p>新的修改后的文本</ p> (关键是我不想使用textareas )。

Here is what I've been trying to do (but it seems to work only with values coming from textareas ) : 这是我一直在尝试做的事情(但它似乎仅适用于来自textareas的值):

<script>
<?php include("js/roman.js"); ?>
function afficher(form2) {
    var testin = document.form2.TextToModify.value;
                 document.form2.NewModifiedText.value=hangul_to_roman(testin);
}
</script>

<div>
    <p name="TextToModify" value="input" onLoad="afficher(form2)">

        TEXT    

    </p>

    <p name="NewModifiedText" value="output">  

        <!-- NEW MODIFIED TEXT   -->

    </p>
</div>

Does anyone know how to get the first text between the "p" tags in order to proceed to the transformation ? 有谁知道如何获取“ p”标签之间的第一个文本以进行转换?

Thank you in advance ! 先感谢您 !

Some notes: 一些注意事项:

  • Better avoid inline event handlers. 最好避免使用内联事件处理程序。
  • p elements can't have name nor value attributes. p元素不能具有namevalue属性。
  • document.form2.TextToModify is a non standard way of accessing a form element. document.form2.TextToModify是访问表单元素的非标准方式。 The standard way would be document.forms.form2.elements.TextToModify , but it won't work because p elements aren't listed elements . 标准方式是document.forms.form2.elements.TextToModify ,但是因为p元素没有列出而不能用。

 function hangul_to_roman(str){ return str; } function afficher() { document.getElementById('NewModifiedText').innerHTML = hangul_to_roman( document.getElementById('TextToModify').innerHTML ); } afficher(); // This must run after loading the DOM 
 <div> <p id="TextToModify"> TEXT </p> <p id="NewModifiedText"> <!-- NEW MODIFIED TEXT --> </p> </div> 

The hangul_to_roman is only included here in order to make the code snippet runnable. hangul_to_roman仅包含在此处,以使代码段可运行。

Note afficher() must be called after your elements have been loaded, eg in a script placed just before closing </body> , or in an event listener of DOMContentLoaded or load events. 注意afficher()必须在元素加载后调用,例如,在关闭</body>之前放置的脚本中,或者在DOMContentLoadedload事件的事件侦听器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM