用javascript改变文本 [英] altering text with javascript
问题描述
亲爱的,
希望我有一个简单的问题。基本上,我只想用JS改变一些文本
。
这是我的一些测试代码:
< snip>
< script type =" text / javascript">
var tmp =''a'';
函数myfunction(){
tmp =''b'';
}
< / script>
< input type =" button" onClick = javascript :myfunction()>
< script type =" text / javascript">
document.write(tmp );
< / script>
< snip>
所以当变量tmp更新时,我想改变''document.write(tmp)''
来反映这一点。
如果有更好的更改文本,请随时提出建议。
非常感谢
Colin
Dear All,
Hopefully I have a simple problem. Basically, I just want to alter some text
with JS.
Here is some of my test code:
<snip>
<script type="text/javascript">
var tmp=''a'';
function myfunction(){
tmp=''b'';
}
</script>
<input type="button" onClick=javascript:myfunction() >
<script type="text/javascript">
document.write(tmp);
</script>
<snip>
So when variable tmp is updated, I want to change the ''document.write(tmp)''
to reflect this.
If there is a better of changing text, feel free to suggest it.
Many thanks
Colin
推荐答案
文章< cj ********** @ ucsnew1.ncl.ac.uk>,
" C Gillespie" < CS ****** @ hotmail.com>写道:
In article <cj**********@ucsnew1.ncl.ac.uk>,
"C Gillespie" <cs******@hotmail.com> wrote:
希望我有一个简单的问题。基本上,我只是想用JS改变一些文本。
以下是我的一些测试代码:
< input type =" button" onClick = javascript :myfunction()>
< script type =" text / javascript">
document.write(tmp);
< / script>
< snip>
Hopefully I have a simple problem. Basically, I just want to alter some text
with JS.
Here is some of my test code: <input type="button" onClick=javascript:myfunction() >
<script type="text/javascript">
document.write(tmp);
</script>
<snip>
1)document.write只能在文档第一时使用
2)在事件处理程序中不需要javascript : Javascript是
假设在所有事件处理程序中,例如onclick。
3)最好在事件中放置javascript代码的引号
处理程序。 onClick =" myfunction();"
4)最好放入尾部分号;。
您需要访问文档节点结构以插入文本。这里
就是一个例子,Robert:
<!DOCTYPE HTML PUBLIC" - // W3C // DTD HTML 4.01 Transitional // EN" ;
" http://www.w3.org/TR/html4/loose.dtd">
< html>
< head>
< title>插入一些文字< / title>
函数changeText(标签,文本)
{
var节点;
if(document.getElementById)
{
var node = document.getElementById(label);
if(node)
{
/ *查看我们是否已插入节点。 * /
var nextNode = node.firstChild;
if(nextNode)
{
/ *是的,替换文字。 * /
nextNode.data = text.value;
}
else
{
/ *否,插入新节点。 * /
node.appendChild(
document.createTextNode(text.value));
node.text = text;
}
}
其他
{
alert("你需要创建一个范围)标记为+
,id为+ label +。;
返回;
}
}
其他
{
alert(" document.getElementByID failed。)+
你需要使用更新的浏览器。;
返回;
}
}
< / script>
< / head>
< body>
< p>插入或更改一些文字。
< br>
别忘了看看会发生什么事情
不止一次按下按钮。< / p>
< form id =" myForm">
< input type =" text" ;命名= QUOT;总" size =" 20">
< br>< br>
< input type =" button"
name ="激活"
value ="更改文字"
onclick =" changeText(''insert'',
文件.forms [''myForm'']。elements [''total'']);">
< br>
< / form>
< p>更改此
< span id =''insert''>
< / span>" 。< / p>
< / body>
< / html>
1) The document.write may only be used when the document is first
loaded.
2) There is no need for javascript: in an event handler. Javascript is
assumed in all event handler such as onclick.
3) It is best to put quotes around the javascript code in an event
handler. onClick="myfunction();"
4) It is best to put in the trailing semi-colon ";".
You need to access the document node structure to insert the text. Here
is an example of this, Robert:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>insert some text</title>
<script type="text/javascript">
function changeText(label,text)
{
var node;
if (document.getElementById)
{
var node = document.getElementById(label);
if (node)
{
/*See if we already inserted a node. */
var nextNode = node.firstChild;
if(nextNode)
{
/* Yes, replace the text. */
nextNode.data = text.value;
}
else
{
/* No, Insert the new node. */
node.appendChild(
document.createTextNode(text.value));
node.text = text;
}
}
else
{
alert("You need to create a span tag with " +
"the id of " + label + ".");
return;
}
}
else
{
alert(" document.getElementByID failed. " +
"You need to use a newer browser.");
return;
}
}
</script>
</head>
<body>
<p>Insert or change some text.
<br>
Don''t forget to see what happens when you
press the button more than once.</p>
<form id="myForm">
<input type="text" name="total" size="20">
<br><br>
<input type="button"
name="activate"
value="change the text"
onclick="changeText(''insert'',
document.forms[''myForm''].elements[''total'']);">
<br>
</form>
<p>Change this "
<span id=''insert''>
</span>".</p>
</body>
</html>
我也试过但是以不同的方式...
希望它有所帮助:
< form>
< input type =" ;文本"名称= QUOT;&会将myText QUOT; value =" a">
< input type =" button"名称= QUOT; myButton的" onClick =" return
myfunction(this.form);">
< / form>
< ; script language =" JavaScript1.2" type =" text / javascript">
function myfunction(frm)
{
frm.myText.value =" b" ;
}
< / script>
如果你不想要a在文本字段中,但只是一个文本我
不知道怎么做...
S.
I tried too but in a different manner...
Hope it helps:
<form>
<input type="text" name="myText" value="a">
<input type="button" name="myButton" onClick="return
myfunction(this.form);">
</form>
<script language="JavaScript1.2" type="text/javascript">
function myfunction(frm)
{
frm.myText.value = "b";
}
</script>
If you don''t want the "a" to be in a text field, but just a text I
don''t konw how to do it...
S.
JRS:文章< rc ***************************** @ news1.west.earthl ink .n
et>,日期为2004年9月24日星期五17:58:55,见于新闻:comp.lang.javascript,
Robert< rc *** ****@my-deja.com>发布:
JRS: In article <rc*****************************@news1.west.earthl ink.n
et>, dated Fri, 24 Sep 2004 17:58:55, seen in news:comp.lang.javascript,
Robert <rc*******@my-deja.com> posted :
您需要访问文档节点结构以插入文本。这是
的一个例子,罗伯特:
You need to access the document node structure to insert the text. Here
is an example of this, Robert:
我认为,这种方式不会像
中的方法那样在浏览器中工作
我们的常见问题解答,4.15。
无需要求getElementById;看看过去的帖子,以及我的
页面js-other.htm,简单地生成它。
-
?约翰斯托克顿,英国萨里。 ?@merlyn.demon.co.uk Turnpike v4.00 IE 4?
< URL:http://www.jibbering.com/faq/> JL / RC:新闻常见问题:comp.lang.javascript
< URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr数学,日期,来源。
< URL:http://www.merlyn.demon.co.uk/> TP / BP / Delphi / jscr /& c,常见问题项目,链接。
That way will, I think, not work in as many browsers as the method in
our FAQ, 4.15.
There is no need to require getElementById ; see past posts here, and my
page js-other.htm , for generating it, briefly.
--
? John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ?
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
这篇关于用javascript改变文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!