JS知識をプッシュしようとしているため、JQueryコードをJavaScriptに変換しようとしています。これで私はx function(){コメントにある}をどこに呼び出すのか分かりません。 JQueryの代わりにJavaScriptにツールチップを追加したいJQueryコードを "vanilla" JSに変換するのに最善の努力をしましたが、いくつかの間違いがありました。私がどこに間違って行ったのか教えていただけますか?JQueryコードを「バニラ」に変換するJavaScript
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>
郵便あなた未遂コード:それと
がCSSがここはるかに適切な、より良い解決策である、言われていますあなたの試みがどこにあるかを質問してください間違っています(どのようなエラーメッセージが表示されますか?また、「* [mcve] *」と「* [ask] *」の両方のガイドラインをお読みください。 –
これは単なるjQueryコードです。あなた自身でこれを解決しようとした証拠はありません。あなたの全体の、動作しているサンプルコードを投稿して、あなたの作品を見せてください。 – SeanKendle
これは試していない..これは、インターネット上の人にあなたのための仕事をさせようとしている。 –