jquery/javascript 中的 php echo() 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8168264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php echo() function in jquery/javascript?
提问by aF.
Is there a jquery/javascript function that does the same that PHP echo()?
是否有与PHP echo()相同的 jquery/javascript 函数?
回答by Rory McCrossan
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()
。
回答by Peter
// 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');
回答by BudwiseЯ
You could use
你可以用
document.write(stringToWrite);
or
或者
document.writeln(stringToWrite).
回答by Tom
I think you're looking for:
我认为您正在寻找:
document.write( ... );
回答by Muhammad Saifuddin
document.write("text"); \javascript syntax.
while using jquery you can view you text under any element obj.
使用 jquery 时,您可以在任何元素 obj 下查看文本。
$("#mydiv").append($("#myElement").val());
回答by Sagar Munjal
I know there is such a simple way to go around it, and we can simply use document.write method. But the downside of that default method is that it simply appends the string passed to the method directly to the body.
我知道有一种简单的方法可以解决它,我们可以简单地使用 document.write 方法。但该默认方法的缺点是它只是将传递给方法的字符串直接附加到主体。
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. 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.
如果我们想要附加特定的标签,我们可以使用我在下面创建的函数。函数名是printThis(),接受三个参数text、className、idName。text 是我们要附加到文档中的文本,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')