在页面加载时使用 javascript 设置段落文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10446499/
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
Set paragraph text with javascript on page load
提问by Pierre Pretorius
Basically all I want to do is set the text of a paragraph using JavaScript/jQuery on page load. This is for a simple Kendo UI app.
基本上我想做的就是在页面加载时使用 JavaScript/jQuery 设置段落的文本。这是一个简单的 Kendo UI 应用程序。
Can anyone please give me a method of doing this?
任何人都可以请给我这样做的方法吗?
I know this is probably a noob question, but I'm semi inexperienced with JavaScript/jQuery.
我知道这可能是一个菜鸟问题,但我对 JavaScript/jQuery 缺乏经验。
This is what I've already tried, but it doesn't seem to be working...
这是我已经尝试过的,但它似乎不起作用......
<script type="">
$(document).ready(function () {
$("#mac001OEE").val("0%");
});
This code has been placed in the head of the HTML page.
此代码已放置在 HTML 页面的头部。
Thanks :)
谢谢 :)
回答by Tats_innit
Please try .text()
good read here: http://api.jquery.com/text/
请尝试.text()
在这里阅读:http: //api.jquery.com/text/
demohttp://jsfiddle.net/FyVzF/13/orfor your specific example using id please see here http://jsfiddle.net/FyVzF/14/
演示http://jsfiddle.net/FyVzF/13/或对于您使用 id 的具体示例,请参见此处http://jsfiddle.net/FyVzF/14/
hope this helps!
希望这可以帮助!
code
代码
$(document).ready(function() {
$("p").text('test test Rambo');
});
HTML
HTML
<p> </p>
回答by Manikandan
Try this only for a particular paragraph with the id "idname":
仅针对 ID 为“idname”的特定段落尝试此操作:
$(document).ready(function() {
$("p#idname").text('test test Rambo');
});
回答by Bibin Velayudhan
$(document).ready(function () {
$("#mac001OEE").html("0%");
});
OR
或者
$(document).ready(function () {
$("#mac001OEE").text("0%");
});
回答by Saeed
Instead of .val()
use .text()
or .html()
:
而不是.val()
使用.text()
or .html()
:
$(document).ready(function() {
$("p").text('saeed');
});
or
或者
$(document).ready(function() {
$("p").html('saeed');
});
回答by VisioN
For non-input elements you should use html
or text
methods:
对于非输入元素,您应该使用html
或text
方法:
$(document).ready(function () {
$("#mac001OEE").html("0%");
});
回答by Rory McCrossan
Assuming you are trying to set the text of a p
element, use text()
:
假设您正在尝试设置p
元素的文本,请使用text()
:
$("#mac001OEE").text("0%");
val()
is used on elements which have a value
attribute, such as input
, select
and textarea
.
val()
用于具有value
属性的元素,例如input
,select
和textarea
。