1

我正在尝试获取鼠标当前所在的元素并将其返回。根据我迄今为止从研究中收集到的信息,这是执行此操作的代码:

    document.getElementById('theTable').onmouseover = function () {

    var x = event.ClientX, y = event.ClientY,
        elementMouseIsOver = document.elementFromPoint(x, y);

    alert(elementMouseIsOver);
}

但是,我创建的所有元素都有 ID,但我总是得到“[object HTMLDivElement]”的不断返回,我希望它告诉我该元素的 ID 是什么......我是否滥用了代码?这将成为 SharePoint 内 Web 部件上的导航栏。

提前感谢您提供任何有用的意见。

所有代码:

var siteUrl = '/sites/dev/';
var theCounter = 0;
ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");

function retrieveListItems() {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('myList');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<Where><And><IsNotNull><FieldRef Name='Title' /></IsNotNull> <IsNotNull><FieldRef Name='TitleLink' /></IsNotNull></And></Where>");
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded),  Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded(sender, args) 
{
var listItemEnumerator = collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) 
    {
    var oListItem = listItemEnumerator.get_current();

    //Each column in in the SharePoint List will essentially become an array.
    //So make an array for each column that will be returned!

    var theHeaders = new Array();
    var HeaderLinks = new Array();
    theCounter += 1;
    theHeaders[theCounter - 1] = oListItem.get_item('Title');
    HeaderLinks[theCounter - 1] = oListItem.get_item('TitleLink');

    //Get the Table Element created in HTML
    var getTheTableTag = document.getElementById('theTable');

    //Create the headers (top level links)
    var createTheHeaderElements = document.createElement('td');
    createTheHeaderElements.id = 'headerTag';

    var link = document.createElement('a');
    link.id = 'headerLinksTag';

    var theHeaderText = document.createTextNode(theHeaders[theCounter - 1]);
    link.setAttribute("href", HeaderLinks[theCounter - 1]);


    link.appendChild(theHeaderText);
    createTheHeaderElements.appendChild(link);
    getTheTableTag.appendChild(createTheHeaderElements);


//        var createA = document.createElement('a');
//        var createAText = document.createTextNode(theCounter);
//        createA.setAttribute('href', "http://google.com");
//        createA.appendChild(createAText);
//        getTheTableTag.appendChild(createA);


};

////////////////////////////HERE IS THE PROBLEM CHILD//////////////////////


document.getElementById('theTable').onmouseover = function () {

    var x = event.ClientX, y = event.ClientY,
        elementMouseIsOver = document.elementFromPoint(x, y);

    alert(elementMouseIsOver);
}

///////////////////////////////////////////////////////////////////////////
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
4

2 回答 2

3

我想到了两件事。

  1. 尝试传递function(event)而不是简单地function()传递onmouseover函数。

  2. event.target.id通常会得到 id - 试试看。

于 2013-08-30T00:42:32.897 回答
1
document.getElementById('theTable').onmouseover = function (e) {
    console.log(e.target)
    alert("mouse is over " + e.target.id);
}
于 2013-08-30T01:11:42.513 回答