简体   繁体   中英

onclick event not firing

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);
}

above is extract of my javascript code. What the code do is to create a table of inviting group from the xml file with accept and decline button. when user press accept, the table will disappear and the table below will move up. For now I am only testing my accept invitation button to see if it works.But my onclick function in the accept button does not work for some reason I don't understand. the alert in acceptInvitation() is not read. Any help will be appreciated. Thanks

What about:

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

This ensures that i is evaluated with the value of the variable instead of as a literal

Try to call it like this

onclick='acceptInvitation();'

Not like this

onclick='acceptInvitation(i)'

while perhaps not addressing the central problem,

onclick='acceptInvitation(i)'

in this case i would be undefined.

onclick='acceptInvitation("+i+")'

would solve at least one problem. Also, you're using an unusual mixture of innerHTML and DOM methods. Why not stick to the DOM methods and use attachEvent/AddEventListener?

edit: A list apart has a good article on binding of variables at http://www.alistapart.com/articles/getoutbindingsituations/

The following is a somewhat specialized example. See the article for more generalized case (or use a library like Prototype )

var click_hdlr = function() {
    return function() {
        return acceptInvitation.apply(this,arguments);
    }
}

var acc_btn = document.createElement("input");
acc_btn.setAttribute("type","button");
acc_btn.setAttribute("id","accept");
acc_btn.setAttribute("value","Accept");
acc_btn.addEventListener("click",click_hdlr(i),false);

不知道这是什么引起您的问题,但是您输出onclick ='acceptInvitation(i)'我想您想输出acceptInvitation(i的值),即acceptInvitation(“ + i +”)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM