简体   繁体   English

如何遍历所有textarea标签并在它们之后添加节点?

[英]How to iterate through all textarea tags and add a node after them?

I'm not a jquery developer and I'm not much of a javascript developer too. 我不是jquery开发人员,也不是javascript开发人员。 I'm trying a quick hack that makes my life simple. 我正在尝试使我的生活变得简单的快速破解。 This is the page: 这是页面: 在此处输入图片说明

What I'm trying to accomplish is: 我想要完成的是:

  1. I want to add a div of mathjax_preview class after each textarea element on the page. 我想在页面上的每个textarea元素之后添加一个mathjax_preview类的div
  2. The inner.HTML of that div must the text inside the corresponding textarea 该div的inner.HTML必须在相应textarea内的textarea

But it doesn't seem to work. 但这似乎不起作用。

I've the following javascript code in js/1.js and it is being loaded when the page loads: 我在js/1.js包含以下javascript代码,并且在页面加载时正在加载它:

var preview_number = 0;
$("textarea").each(function() { 
    var d = $(this).next();
    if (!d.hasClass("mathjax_preview")) {
      preview_number++;
      var d = $("<div class='mathjax_preview' " +
         "style='padding: 5px; color: black; background: #eee; border: 1px solid #888;float:left;'" +
         "></div>");
      d.attr("id", "mathjax_preview_" + preview_number);
      d.insertAfter(this);
    } 
    d.text($(this).val());
    MathJax.Hub.Queue([ "Typeset", MathJax.Hub, d.attr("id") ]);
});

EDIT: 编辑:

I just inserted following snippet in the beginning of the above file: 我只是在上述文件的开头插入了以下代码段:

var preview_number = 0;
var elements = document.getElementsByTagName("textarea");
  alert(elements.length);

Its alerting 0 . 其警报0 How come? 怎么会?

Try the following (I've not tested this as it's straight off the top of my head): 尝试以下操作(我尚未对其进行测试,因为它就在我的头顶上):

$(document).ready(function() {
    var preview_number = 0;

    $("textarea").each(function() { 
        preview_number++;

        var d = document.createElement("div");
        $(d).attr("id", "mathjax_preview_" + preview_number);
        $(d).addClass("mathjax_preview");
        $(d).css("padding", "5px");
        $(d).css("color", "black");
        $(d).css("background", "#eee");
        $(d).css("border", "1px solid #888");
        $(d).css("float", "left");
        $(d).html($(this).val());

        MathJax.Hub.Queue([ "Typeset", MathJax.Hub, $(d).attr("id") ]);

        $(this).append(d);
    });
});

The variable textarea does not exist. 可变的textarea不存在。 Use this instead: 使用this代替:

d.insertAfter(this);

You have no textarea variable defined. 您没有定义的textarea变量。

d.insertAfter(textarea);

I beleive you can use $(this) reference. 我相信您可以使用$(this)参考。

d.insertAfter($(this));

What is textarea in d.insertAfter(textarea); d.insertAfter(textarea); textarea是什么? ?

It seems to be d.insertAfter($(this)); 好像是d.insertAfter($(this)); .

The alert message is showing you zero because you have not put your javascript file at the end of page nor you have used ready callback. 警报消息显示为零,因为您尚未将javascript文件放在页面末尾,也未使用就绪回调。 To use ready callback add the code as follows 要使用就绪的回调,请添加以下代码

$(document).ready(function() {/*add your javascript code here */}); $(document).ready(function(){/ *在此处添加您的JavaScript代码* /});

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

相关问题 如何遍历数组中的所有数字,然后加上或减去它们,使它们达到预定值 - How can you iterate through all the numbers in an array and either add or subtract them so they hit a predetermined value 如何添加 在所有浏览器的文本区域中将标签标记为选定文本? - How to add <b></b> <i></i> tags to selected text in a textarea in all browsers? 迭代所有集合并删除它们 - Iterate through all collections and remove them 如何将所有选定的元素添加到数组中并对其进行迭代? - How to add all selected elements into an array and iterate through it? 如何将节点添加到文本区域的末尾 - How to add a node to the end of the textarea 如果textarea是动态的,如何从textarea中删除所有html标签? - How to remove all html tags from textarea if textarea is dynamic? 遍历所有html标记,包括Javascript中的子代 - Iterate through all html tags, including children in Javascript 如何从所有 [pre] 标签中找出所有 [0-9,az,AZ] 并为它们添加颜色? - How to find out all [0-9,a-z,A-Z] from all [pre] tags, and add color for them? 有没有办法遍历数组中的日期并将它们添加到这个对象? - Is there a way I could iterate through dates in an array and add them to this object? 如何使用节点和 cheerio 遍历表 - how to iterate through a table using node and cheerio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM