2 备注:
<input... >
tag 不是一个标签,应该被关闭,<input ... />
否则你应该使用这个<button>
标签(但不是出于 X 浏览器合规性的原因)
如果您不想进入一个没有结束的故事,您可以重新考虑处理您的观点的方式如下:保持<div>
原样并在其上触发一个 DOM 事件(而不是使用标签上的 onclick 事件) . 如果你会读法语,我在这里总结了很多例子。这种方式比你的第一种方式要重得多,但之后更强大,因为你可以在 DOM 上按你想要的方式触发事件,使用回调函数等等。
原理如下:
一方面是HTML页面
<div id="test"...>Lorem..</div>
<script src="..."/>
另一方面,非侵入式 javascript 代码
// your event handler here
function Dothisorthat(e) {
// pop up the dom content or edit it or start an ajax request or whatever you need here
return false;
}
// This is read in the third place
function attacherAction() {
// Attach the action "Dothisorthat" on the event "onclick" occuring on your div "test"
id ='test'; // your div id
if (document.getElementById(id)) {
zLink = document.getElementById(id);
zLink.onclick = function() {
Dothisorthat(this); // in your case, you can write a small function to pop up or edit the div content
}
}
}
// This is read second
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
// This is read first
addLoadEvent(attacherAction);
希望有帮助