由JS设置的innerHTML的跨度ID? [英] Span id with innerHTML set by JS ?

查看:303
本文介绍了由JS设置的innerHTML的跨度ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



遵循之前的问答,并感谢我的回答.但是,进一步的测试显示了一个相关的问题,如测试代码中所示.单击深"时,跨度ID"box2"将显示正确的结果,但单击粉红色"时,它将闪烁结果,但结果将不保留.为什么?

我还指出了两件事:使用的效果:

Hi,

Follow the previous Q/A and thanks to the answer I got. However further test showed a related issue as shown in the test code. The Span ID ''box2'' will show the correct result when click Dark but when click Pink, it flash the result but the result would not stay. Why?

I also noted two things: the effect of using:

document.all.box2.innerHTML = s;
    document.getElementById("box2").innerHTML = s;



看起来一样并且span.id不能由document.f.box2引用.该错误将再次为Null对象.为什么会有这样的不一致?




seems the same; and the id in span can not be referred by document.f.box2. The error would again be Null object. Why such inconsistency?


<script language="javascript" type="text/javascript">
function putText(s) {
alert(s);
    document.f.box1.value = s;

    // the two statements have same effect
    //document.all.box2.innerHTML = s;
    document.getElementById("box2").innerHTML = s;

    // can not have this, it does not affect input id but it does affect span id
    //document.f.submit();
}
</script>

<body>
<%
box1 = request("box1")
'box2 = request("box2")
if box1 = "" then
    box1 = "The sky is blue today"
    end if


%>

<form name="f" id="f" method="post">
<input name="box1" id="box1" type="text" value="<%=box1%>" />
<br>Box2=<span id="box2"></span>
<br>
<input type="submit" value="Pink" onClick="putText('Click to Pink Sky')"  />
<input name="test" type="submit" value="Dark"  />

<%
if request("test") = "Dark" then
    response.Write("<script>putText('Submit to get dark sky')</script>")
    end if
%>

</form>
</body>



rgds,
kfl.



rgds,
kfl.

推荐答案

输入类型为Submit,这将导致在执行javascript之后将表单提交回服务器,这将导致页面使用默认值重新渲染.

删除type=''submit''或使用onClick="putText(''Click to Pink Sky''); return false;"
The input type is submit which is going to cause the form to be submitted back to the server after the javascript is executed, which will cause the page to be re-rendered with the default values.

Either remove the type=''submit'' or use onClick="putText(''Click to Pink Sky''); return false;"


这篇关于由JS设置的innerHTML的跨度ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆