简体   繁体   English

使用javaScript将String输出到HTML

[英]Outputting String to HTML using javaScript

there have been many questions like this before, but the answer usually involved referencing the element to be outputted to within the script. 之前有很多这样的问题,但答案通常涉及引用要在脚本中输出的元素。 in my case, elements are created dynamically on load (it's hosted on Tumblr, to be specific), so there's no referable ID for each of the elements I need to output a string to. 在我的例子中,元素是在加载时动态创建的(它托管在Tumblr上,具体而言),因此我需要输出字符串的每个元素都没有可参考的ID。

is it possible to do this? 是否有可能做到这一点? jQuery can be used if it makes things much simpler. 如果它使事情变得更简单,可以使用jQuery。 my function takes a string as an input and outputs a string using return . 我的函数将一个字符串作为输入,并使用return输出一个字符串。

$('.post .posttext a').html(YourfunctionThatReturnsAString());

这应该做到这一点

$('.post .posttext a') -> this returns an array of a elements. $('.post .posttext a') - >这会返回a元素数组。

$('.post').eq(the post number).find('.posttext a') -> replace "the post number" with the number of your post if you want to target something more specific. $('.post').eq(the post number).find('.posttext a') - >如果你想要更具体的目标,用你的帖子号码替换“帖子号码”。

You can then call the text("your text") method to set the text. 然后,您可以调用text("your text")方法来设置文本。

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

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