[英]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: 我想要完成的是:
div
of mathjax_preview class after each textarea
element on the page. textarea
元素之后添加一个mathjax_preview类的div
。 textarea
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.