gpt4 book ai didi

javascript onmouseout 清除html元素

转载 作者:行者123 更新时间:2023-11-28 02:00:26 26 4
gpt4 key购买 nike

我试图弄清楚如何清除 JavaScript onmouseleave 事件上特定 HTML ID 的内容。我的 onmouseover 工作得很好,但 onmouseout 却不行。

这是一个导航栏(正在进行中),因此当用户将鼠标悬停在标题上时,它将显示子链接,但当他们离开该菜单时,它应该关闭。

我所研究的一切都没有让我得到任何结果...我正在寻找有关如何完成此场景的 onmouseout 函数的提示。

谢谢!

这是我得到的:

     document.getElementById('theHeaderTag1').onmouseover = function () {
for (var i = 0; i <= Level1Items.length - 1; i++) {
//Print Items in Level1
var createLevel1CellTag = document.createElement('tr');
createLevel1CellTag.id = 'Level1';
var Level1TextNode = document.createTextNode(Level1Items[i]);
createLevel1CellTag.appendChild(Level1TextNode);
document.getElementById('theHeaderTag0').appendChild(createLevel1CellTag);
}
document.getElementById('theHeaderTag1').onmouseout = function () {

}

我有一种感觉,虽然我会遇到这个问题......但我认为这会让我走上正轨。

这是所有代码:

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


//Where all the magic happens
function onQuerySucceeded(sender, args) {

//Create an Array for each column in the SharePoint List that will be used in the NavPart
var theHeaders = new Array();
var HeaderLinks = new Array();
var Level1Items = new Array();
var getTheTableTag = document.getElementById('theTable');

var listItemEnumerator = collListItem.getEnumerator();

while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
theCounter += 1;

//Build an Array for the column elements.
theHeaders[theCounter - 1] = oListItem.get_item('Title');
Level1Items[theCounter - 1] = oListItem.get_item('Level1');



};
var headersResult = _.uniq(theHeaders);
var headerLinkResult = _.uniq(HeaderLinks);

//Create Headers on the Web Part
for (var i = 0; i <= headersResult.length - 1; i++) {
var createTheHeaderTag = document.createElement('th');
createTheHeaderTag.id = 'theHeaderTag' + i;
var TheHeaderTagTextNode = document.createTextNode(headersResult[i]);
createTheHeaderTag.appendChild(TheHeaderTagTextNode);
getTheTableTag.appendChild(createTheHeaderTag);


}

document.getElementById('theHeaderTag1').onmouseover = function () {
for (var i = 0; i <= Level1Items.length - 1; i++) {
//Print Items in Level1
var createLevel1CellTag = document.createElement('tr');
createLevel1CellTag.id = 'Level1';
var Level1TextNode = document.createTextNode(Level1Items[i]);
createLevel1CellTag.appendChild(Level1TextNode);
document.getElementById('theHeaderTag0').appendChild(createLevel1CellTag);
}
document.getElementById('theHeaderTag1').onmouseout = function () {

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

最佳答案

代码看起来不错。您需要在鼠标移开时删除或隐藏 DOM 元素。

编辑->演示:http://jsfiddle.net/uENE7/

document.getElementById('theHeaderTag1').onmouseout = function () {
document.getElementById('theHeaderTag0').removeChild("Level1")
}

要检查您的 mouseout 事件是否被触发,请添加控制台日志消息。

document.getElementById('theHeaderTag1').onmouseout = function () {
console.log("mouseout event triggered");
document.getElementById('theHeaderTag0').removeChild("Level1");
}

关于javascript onmouseout 清除html元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18524229/

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