我在.cshtml文件中有以下代码:

var peopleList = $('#PeopleListTable').dataTable({
                    // not relevant
                    "fnRender": function (oObj) {
                        var documentiddata = oObj.aData[0];
                        var notesdata = (oObj.aData[2]);
                        //alert(notesdata);

                        if (notesdata != null) {
                            var image = "images/AR/Check-on.png";
                            // return '<a href="#"  id="' + notesdata + '" onclick="return ShowNotes(this);">' + '<img  src="' + image + '" />' + '</a>';
                            return '<p><a onmouseout="return hideNotePopup();" onmouseover="return showNotePopup(notesdata, event);" href="javascript:void(0);" id="' + documentiddata + '">' + '<img  src="' + image + '" />' + '</a></p>'
                        } else {
                            return '<a href="#" id="' + documentiddata + '"> ' + '<img src="images/AR/Check-off.png" />' + '</a>';
                        }
                    }
                    },
                { "sName": "OfficerName", sType: "string", sWidth: "12%" },
                 { "sName": "CreateDate", sType: "string", sWidth: "15%" },
                 { "sName": "FinalizedDate", sType: "string", sWidth: "15%" },
                 { "sName": "TransferDate", sType: "string", sWidth: "15%" },
                 { "sName": "AgencyOri", sType: "string", sWidth: "10%" }
            ]
        });


然后使用JavaScript编写以下代码:

function showNotePopup(notesdata, e) {
        $("#NoteDialog").dialog('close');
        $("#NoteDialog").removeClass("ui-icon ui-icon-closethick");
        $("#NoteDialog").dialog({
            autoOpen: false,
            modal: true,
            resizable: false,
            position: [e.pageX, e.pageY-190]
        });
        $("#NoteDialog").dialog('open');
        document.getElementById("note").innerHTML = notesdata;
    }


这段代码的目标是将鼠标悬停在数据表中的便笺图像上,然后弹出一个显示便笺内容的窗口。我有alert(notesdata)的地方,注释显示正确。但是,当我将鼠标悬停在图像上并检查控制台时,它说在notesdata调用中未定义showNotePopup()。我也尝试通过thisoObj都无济于事。如何从cshtml内获取notesdata到javascript函数?

最佳答案

变量notesdata仅存在于函数fnRender的上下文中。事件onmouseover在不同的上下文中执行,因此变量不在范围内。你需要改变

onmouseover="return showNotePopup(notesdata, event);"


通过

onmouseover="return showNotePopup(&quot;' + notesdata + '&quot;, event);"

10-01 10:22