简体   繁体   English

jquery / javascript中的php echo()函数?

[英]php echo() function in jquery/javascript?

是否有一个jquery / javascript函数与PHP echo()相同?

I suppose the closest is document.write() as it's native javascript. 我想最接近的是document.write()因为它是原生的javascript。

However there are many methods of writing/amending text in the DOM, such as innerHtml() , innerText() and outerHtml() , as well as jQuery's html() and text() . 但是,在DOM中编写/修改文本有很多方法,例如innerHtml()innerText()outerHtml() ,以及jQuery的html()text()

You could use 你可以用

document.write(stringToWrite);

or 要么

document.writeln(stringToWrite).
// This is what you are looking for
document.write('Hello world');
document.getElementById('mydiv').write('Hello world');

// jQuery
$(body).append('Hello world');
$('#mydiv').append('Hello world');

我想你正在寻找:

document.write( ... );
document.write("text"); \\javascript syntax.

使用jquery时,您可以在任何元素obj下查看文本。

 $("#mydiv").append($("#myElement").val());

I know there is such a simple way to go around it, and we can simply use document.write method. 我知道有这么简单的方法可以解决它,我们可以简单地使用document.write方法。 But the downside of that default method is that it simply appends the string passed to the method directly to the body. 但是这种默认方法的缺点是它只是将传递给方法的字符串直接附加到正文。

In case we want specific tags to be appended we can use the function i made below. 如果我们想要附加特定标签,我们可以使用下面的功能。 The function name is printThis() and takes three parameters text, className, idName. 函数名称为printThis(),并带有三个参数text,className,idName。 The text is the text which we want to append to the document, the className and idName are the name of the class and id we want to set. 文本是我们要附加到文档的文本,className和idName是我们要设置的类和id的名称。

 function printThis (text,className,idName){ var p = document.createElement('p') var t = document.createTextNode(text); setAttribute = function (el, attrs) { for(var key in attrs) { el.setAttribute(key, attrs[key]); } } setAttribute(p,{class:className,id:idName}) p.appendChild(t) document.body.appendChild(p) } printThis('This is a new Paragraph','newClass','newId') 

If you want to edit the content of an element use html() . 如果要编辑元素的内容,请使用html() You can manipulate the DOM in diffrent ways. 您可以以不同的方式操纵DOM。 Check the docs . 检查文档

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

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