我正在使用带有缩略图的awesomplete autocomplete plugin并且我有index.html和我<input id="myinput">
元素,但是如果我没有放置{{{{}}我的laoyut.html 1}}然后js给我一个这个错误
<input id="myinput">
我的问题是我应该怎么做才能解决它?
jquery.min.js:2 Uncaught TypeError: Cannot read property 'offsetWidth' of undefined
&#13;
var input = document.getElementById("myinput");
// Show label but insert value into the input:
new Awesomplete(input, {
list: [{
label: "<img src='https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRUxdD-Q4nIx3uIg9jBCe1oT5a9MHuWY5_pW4FoZSU-nQd1Y_WJPQ'/> Faceboock",
value: "https://www.facebook.com/"
},
{
label: "<img src='https://hydra-media.cursecdn.com/dota2.gamepedia.com/thumb/2/25/Pounce_icon.png/16px-Pounce_icon.png?version=77c984fc4a9c8ca491ead081322fa738'/> Youtube",
value: "https://www.youtube.com/"
},
{
label: "China",
value: "CN"
},
{
label: "United States",
value: "US"
}
]
});
// You can search for a better version
$(document).find('.awesomplete').on('click', function(e) {
if ($('#myinput').val())
window.location = $('#myinput').val();
//console.log($('#myinput').val());
});
&#13;
答案 0 :(得分:1)
您可以先检查html dom中是否有#myinput,然后您可以使用它的值。
var input = $("#myinput");
if (input.size() > 0) {
window.location = input.val();
}
我认为将有一个具有此特定ID的div。在这种情况下,您可以使用
if (input.size() === 1) {
简而言之,您可以替换
$(document).find('.awesomplete').on('click', function(e) {
if ($('#myinput').val())
window.location = $('#myinput').val();
//console.log($('#myinput').val());
});
与
$(document).find('.awesomplete').on('click', function(e) {
var input = $('#myinput');
if (input.size() === 1) {
window.location = input.val();
}
});