gpt4 book ai didi

javascript - 将 JQuery 代码转换为 "Vanilla"JavaScript

转载 作者:行者123 更新时间:2023-12-03 03:05:38 27 4
gpt4 key购买 nike

我正在尝试提升我的 JS 知识,因此我正在尝试将 JQuery 代码转换为 JavaScript。在此我不知道在哪里调用 x function(){which is in comment}。想要将工具提示添加到 JavaScript 而不是 JQuery。我尽力将 JQuery 代码转换为“vanilla”JS,但犯了一些错误。你能告诉我我哪里错了吗?

var persons = [{
firstname: "Malcom",
lastname: "Reynolds",
id: 1
}, {
firstname: "Kaylee",
lastname: "Frye",
id: 2
}, {
firstname: "Jayne",
lastname: "Cobb",
id: 3
}, {
firstname: "Mal",
lastname: "Rlds",
id: 4
}, {
firstname: "ylee",
lastname: "ye",
id: 5
}, {
firstname: "yne",
lastname: "Cb",
id: 6
}, {
firstname: "Mal",
lastname: "Rolds",
id: 7
}, {
firstname: "lee",
lastname: "Fryedwwwwd",
id: 8
}, {
firstname: "Jay",
lastname: "bdwe",
id: 9
}, {
firstname: "Maljh",
lastname: "Rolds",
id: 10
}, {
firstname: "ljyyu",
lastname: "jhjFryed",
id: 11
}, {
firstname: "ryJay",
lastname: "jkmCdwe",
id: 12
}, {
firstname: "yuryMal",
lastname: "jhkhRolds",
id: 13
}, {
firstname: "rtulee",
lastname: "Frwwd",
id: 14
}, {
firstname: "ryuyJay",
lastname: "Cryyobbdwe",
id: 15
}];

var start = 1;
var size = 5;
var current = 0;

function getFullName(item, index) {
return "<li>" + item.firstname + "<span class='tool-tip'>" + item.lastname + "</span> " + item.id + "</li>";
}

var getFullNameForHighlighting = function(searchText) {
return function(item, index) {
var index = item.firstname.toUpperCase().indexOf(searchText.toUpperCase())+searchText.length;
console.log(index)
return "<li>" + "<span class='highlight'>"+searchText + "</span>" +item.firstname.substring(index) + "<span class='tool-tip'>" + item.lastname + "</span> " + item.id + "</li>";
}
};

function myFunction1(){
current = size;
document.getElementById("myUL").innerHTML = persons.map(getFullName).slice(start, current).join('');
}

function myFunction() {
current = size;
var input, filter, ul, li, a, i, count = 0;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();

var longWords = persons.filter(function(person) {

return person.firstname.toUpperCase().indexOf(filter) > -1
});
var message = "Result:" + longWords.length + " words containing '" + input.value;
document.getElementById("demo").innerHTML = message;

ul = document.getElementById("myUL");
varInnerHtmlText = longWords.map(getFullNameForHighlighting(filter));
ul.innerHTML = varInnerHtmlText;

highlight();
}

function highlight() {

var inputText, filter1, innerHTML, index;
inputText = document.getElementById("myInput");
console.log(inputText);
filter1 = inputText.value.toUpperCase();
console.log(filter1);
innerHTML1 = inputText.innerHTML;
var ul = document.getElementById("myUL");
console.log(innerHTML1);
index = innerHTML1.indexOf(filter1);

if (index >= 0) {
innerHTML1 = innerHTML1.substring(0, index) + "<span class='highlight'>" + innerHTML1.substring(index, index + filter1.length) + "</span>" + innerHTML1.substring(index + filter1.length);
filter1.innerHTML = innerHTML1;
}
}
var x = 0;
function myFunction3(e) {
console.log(e);
document.getElementById("demo").innerHTML = ++x;
}
* {
box-sizing: border-box;
}

#myInput {
width: 50%;
font-size: 14px;
padding: 8px 10px 8px 5px;
border: 1px solid #ddd;
margin-bottom: 12px;
margin-top: 20px;
}

#clear {
margin-left: 20px;
margin-top: 40px;
font-size: 14px;
margin-right: 50px;
padding: 4px 4px 4px 4px;
}

#myUL {
list-style-type: none;
padding: 0;
margin: 0;
}

#myUL li a {
border: 1px solid #ddd;
margin-top: -1px;
/* Prevent double borders */
background-color: #f6f6f6;
padding: 8px;
text-decoration: none;
font-size: 16px;
color: black;
display: block;
width: 100%;
}

#myUL li a:hover:not(.header) {
background-color: #eee;
}

body {
margin: 0;
}

.column {
float: left;
margin-top: 50px;
padding: 10px;
height: 550px;
}

.left {
width: 20%;
}

.right {
width: 60%;
}

.row:after {
content: "";
display: table;
clear: both;
}

ul.demo {
display: inline-block;
border-bottom: 1px dotted black;
}

ul.demo li {
position: right;
}

.tool-tip {
display: none;
width: 100px;
background-color: white;
color: #fff;
text-align: center;
border-radius: 2px;
position: absolute;
z-index: 1;
color: black;
}

.highlight {
font-weight: bold;
color: black;
}

li em {
background: #ff6;
font-weight: bold;
font-style: normal;
}
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="jquery.jscroll.js"></script>
<script src="two.js"></script>



<body onload="myFunction1()">
<br>
<br>

<div class="row">
<div class="column left" style="background-color:White;"></div>

<div class="column right" style="background-color:#bbb;">
<form id="myForm" onsubmit='return onSubmit(this)'>

Find:
<input type="text" class="demo1" id="myInput" align="center" onkeyup="myFunction()" placeholder="Search here...">

<button id="clear" onclick="document.getElementById('myInput').value = ''"> Clear </button>

</form>
<br>
<ul id="demo">
15 words total
</ul>
<br>
<div onscroll="myFunction3(this)" style="width:100%;height:250px;overflow:scroll;padding:5px;color:#714D03;scrollbar-base-color:#DEBB07;">

<ul id="myUL" class="demo">

</ul>
</div>
<p><span id="demo"></span></p>
</div>
</div>
<script>
$('#myUL').on("mouseenter", "li", function() {
$(this).find("span").show();
})

$('#myUL').on("mouseout", "li", function() {
$(this).find("span").hide();
})

</script>

<script>
// function x()
// {
// var ul = document.getElementById("myUL").getElementsByTagName('li');
// console.log(ul);

// ul.addEventListener('mouseenter', function(e) {
// if ( e.target.matchesSelector('myUL li') ) {
// return "<li>" + item.firstname + "<span class='tool-tip'>" + item.lastname + "</span> " + item.id + "</li>";
// }
// }, false);

// ul.addEventListener('mouseout', function(e) {
// if ( e.target.matchesSelector('myUL li') ) {
// return "<li>" + item.firstname + item.id + "</li>";
// }
// }, false);
// }

</script>

最佳答案

要在 native JS 中执行此操作,您可以使用 querySelectorAll() 查找元素,然后使用 addEventListener() 添加事件处理程序。另请注意,mouseleave 是在这里使用的更好的事件,因为它可以避免在 mouseout 事件下显示子元素时可能遇到的一些闪烁问题。试试这个:

var liCollection = document.querySelectorAll('#myUL li');
[].forEach.call(liCollection, function(li) {
li.addEventListener('mouseenter', function() {
this.querySelector('span').style.display = 'inline';
});
li.addEventListener('mouseleave', function() {
this.querySelector('span').style.display = 'none';
});
})
span { display: none; }
<ul id="myUL">
<li>Foo <span>bar</span></li>
<li>Lorem <span>impsum</span></li>
<ul>

话虽如此,CSS 在这里是一个更合适、更好的解决方案:

li span { display: none; }
li:hover span { display: inline; }
<ul id="myUL">
<li>Foo <span>bar</span></li>
<li>Lorem <span>impsum</span></li>
<ul>

关于javascript - 将 JQuery 代码转换为 "Vanilla"JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47182285/

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