gpt4 book ai didi

javascript - onclick 事件未触发

转载 作者:行者123 更新时间:2023-12-02 20:47:05 25 4
gpt4 key购买 nike

var subjectList;

function PageMaster()
{
this.contentDiv = document.getElementById("content");
}

/**
* Builds the main part of the web page based on the given XML document object
*
* @param {Object} xmlDoc the given XML document object
*/

PageMaster.prototype.doIt = function(xmlDoc)

{
alert("PageMaster()");

alert("Clear page...");
this.contentDiv.innerHTML = "";

if (null != xmlDoc)
{
alert("Build page...");

//create div Post
var divPost = document.createElement("div");
divPost.className = "post";

//create h1 element
var h1Element = document.createElement("h1");
var headingText = document.createTextNode("Invitations");
h1Element.appendChild(headingText);

//insert h1 element into div post
divPost.appendChild(h1Element);

subjectList = xmlDoc.getElementsByTagName("subject");
var groupList = xmlDoc.getElementsByTagName("group");

for (var i = 0; i < subjectList.length; i++) //for each subject
{
var divEntry = document.createElement("div");
divEntry.className = "entry";

var subjectNum = subjectList[i].attributes[0].nodeValue;
var subjectName = subjectList[i].attributes[1].nodeValue;
var groupId = groupList[i].attributes[0].nodeValue;
var groupName = groupList[i].attributes[1].nodeValue;
var ownerId = groupList[i].attributes[2].nodeValue;

//set up the invitation table attributes
var table = document.createElement("table");
table.width = 411;
table.border = 3;
table.borderColor = "#990000"

tableRow = table.insertRow(0);
tableCell = tableRow.insertCell(0);

var cellContent = "";

//create invitation message
var invitationMsg = "<p>Subject : " + subjectNum + " - " + subjectName + "</p>";
invitationMsg += "<p>You are invited to join " + groupName + " (groupId : " + groupId + ") by owner Id:" + ownerId + "</p>";
cellContent += invitationMsg;

//create buttons
cellContent += "<input type='button' id='acceptButton" + i + "' value='Accept' onclick='acceptInvitation(i)'>"
cellContent += "<input type='button' id='declineButton" + i + "' value='Decline'>"

tableCell.innerHTML = cellContent;

divEntry.appendChild(table);

var blankSpace = document.createElement("p");
divEntry.appendChild(blankSpace);
divPost.appendChild(divEntry);
}

//insert div post into div content
this.contentDiv.appendChild(divPost);
}
};

function acceptInvitation(i)
{
alert("hello");
//alert(subjectList[i].attributes[0].nodeValue);
}

上面是我的 JavaScript 代码的摘录。该代码的作用是从 xml 文件创建一个邀请组表,并带有接受和拒绝按钮。当用户按下接受键时,表格将消失,并且下面的表格将向上移动。目前我只是测试我的接受邀请按钮,看看它是否有效。但是由于某种我不明白的原因,我的接受按钮中的 onclick 功能不起作用。未读取acceptInvitation() 中的警报。任何帮助将不胜感激。谢谢

最佳答案

关于:

cellContent += "[...] onclick='acceptInvitation("+i+")'>"

这确保了 i 是用变量的值而不是文字来计算的

关于javascript - onclick 事件未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/909157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com