我希望这个话题符合这个问题。
嘿,伙计们,请原谅我的傲慢,但我一直在绞尽脑汁解决这个问题。
代码:
<a id="123" href="#123" onclick="document.getElementById('chgtext').innerHTML='<a id="1" href="#" onclick="document.getElementById('chgtext_1').innerHTML='Johnss's House';">chapter 1</a>';">Wonderful</a>
<div id="chgtext"> </div>
<div id="chgtext_1"> </div>我想要的是显示一个叫做“精彩”的链接,当点击它时,它会显示“第一章”,当“第一章”被点击时,它会显示"Johnss's“
我试着用\来摆脱这一切,但不起作用。
如果可能的话,我希望它仍然是这个方法,但是如果这个方法不起作用,那么有没有其他方法。
哦,还有整个链接
<a id="123" href="#123" onclick="document.getElementById('chgtext').innerHTML='<a id="1" href="#" onclick="document.getElementById('chgtext_1').innerHTML='Johnss's House';">chapter 1</a>';">Wonderful</a>从php服务器端输出到HTML以将其回显出来。这就是为什么它在一行代码中
谢谢
发布于 2018-06-20 16:34:38
尝尝这个
<!DOCTYPE html>
<html>
<body>
<a href="#" onclick="main()">Wonderful</a>
<p id="main"></p>
<p id="sub"></p>
<script>
function main(){
document.getElementById("main").innerHTML =
"<a href='#' onclick='sub()'>Chapter 1</a>";
}
function sub(){
document.getElementById("sub").innerHTML =
"Johnss's";
}
</script>
</body>
</html>
发布于 2018-06-20 16:41:49
我将使用事件处理程序和bind the events using js,而不是在html中,这样,您就可以为尚未创建的动态对象执行delegate an event
// bind a normal event to the first link:
var link = document.getElementById('123');
link.addEventListener('click', function (e) {
e.preventDefault(); // as the target is a link you want to prevent the default action of it
document.getElementById('chgtext').innerHTML = '<a id="1" href="#">chapter 1</a>';
});
// the following is a delegated event - you bind it to an existing element (in this case the document) and then check that the target being clicked is the new element that has been dynamically created
document.addEventListener('click', function (e) {
// check the id of the clicked element
if (e.target.id == 1) {
e.preventDefault(); // as the target is a link you want to prevent the default action of it
document.getElementById('chgtext_1').innerHTML = 'Johns\'s House';
}
});<a id="123" href="#123">Wonderful</a>
<div id="chgtext"> </div>
<div id="chgtext_1"> </div>
https://stackoverflow.com/questions/50943567
复制相似问题