Javascript 我如何处理这个AJAX(?)bug?
所以我有一个bug,每次我试图点击一个元素(由我使用jquery生成的自动完成函数生成),我只能在中间点击才能访问它,但是,如果我使用箭头&每次输入都有效。有什么想法吗?我的密码是。也许我只是看不到,但我看了几个小时Javascript 我如何处理这个AJAX(?)bug?,javascript,Javascript,所以我有一个bug,每次我试图点击一个元素(由我使用jquery生成的自动完成函数生成),我只能在中间点击才能访问它,但是,如果我使用箭头&每次输入都有效。有什么想法吗?我的密码是。也许我只是看不到,但我看了几个小时 {literal} $(document).ready(function() { function autocomplete(inp) { var currentFocus; var arr =[]; inp.on("input",
{literal}
$(document).ready(function() {
function autocomplete(inp) {
var currentFocus;
var arr =[];
inp.on("input", function(e) {
var a, b, i, val = this.value;
closeAllLists();
if (!val) { return false;}
currentFocus = -1;
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
this.parentNode.append(a);
$.ajax({
type:"POST",
url:"//",
dataType: 'json',
data: {user_id: __st.a, keyword: val},
success: function(products) {
if(products.length){
products.forEach(function (product, index) {
b = document.createElement("div");
var div_class = "bundle_autocomplete_item_"+index;
b.setAttribute("class", div_class);
b.innerHTML = '<span style="float: left; display: block; width: 20%;"><img src="' + product.image_url + '" alt="productImageSrc"></span><span class="autocomplete-title">' + product.title + '</span>';
$(b).on("click", function(e) {
closeAllLists();
window.location.href = 'https://'+Shopify.shop+'/products/'+product.handle;
});
a.appendChild(b);
});
}
}
});
});
inp.on("keydown", function(e) {
var x = document.getElementById(this.id + "autocomplete-list");
if (x) x = x.getElementsByTagName("div");
if (e.keyCode == 40) {
currentFocus++;
addActive(x);
} else if (e.keyCode == 38) { //up
currentFocus--;
addActive(x);
} else if (e.keyCode == 13) {
e.preventDefault();
if (currentFocus > -1) {
if (x) x[currentFocus].click();
}
}
});
function addActive(x) {
if (!x) return false;
removeActive(x);
if (currentFocus >= x.length) currentFocus = 0;
if (currentFocus < 0) currentFocus = (x.length - 1);
x[currentFocus].classList.add("autocomplete-active");
}
function removeActive(x) {
for (var i = 0; i < x.length; i++) {
x[i].classList.remove("autocomplete-active");
}
}
function closeAllLists(elmnt) {
var x = document.getElementsByClassName("autocomplete-items");
for (var i = 0; i < x.length; i++) {
if (elmnt != x[i] && elmnt != inp) {
x[i].parentNode.removeChild(x[i]);
}
}
}
}
var searchForms = $('form[action="/search"]').each(function() {
$('input[name="q"]').each(function() {
$(this).attr("autocomplete" , "off");
autocomplete($(this));
});
});
$('head').append('<style> .autocomplete {position: relative;display: inline-block;border:10px solid red;} .autocomplete-items {position: absolute;border: 1px solid #d4d4d4;border-bottom: none;border-top: none;z-index: 99;top: 100%;left:0;right: 0;overflow:hidden;white-space:nowrap;} .autocomplete-items div {padding: 7px ;cursor: pointer;background-color: #fff; border-bottom: 1px solid #d4d4d4;text-overflow:ellipsis; white-space:nowrap; overflow:hidden;}} .autocomplete-items div:hover {background-color: #e9e9e9;}.autocomplete-active {background-color: DodgerBlue !important; color: #ffffff;} .autocomplete-title{font-size:13px; margin-left:3px;</style>');});
{/literal}
{literal}
$(文档).ready(函数(){
函数自动完成(inp){
无功电流聚焦;
var-arr=[];
输入打开(“输入”,功能(e){
var a,b,i,val=该值;
closeAllList();
如果(!val){返回false;}
currentFocus=-1;
a=document.createElement(“DIV”);
a、 setAttribute(“id”,this.id+“自动完成列表”);
a、 setAttribute(“类”、“自动完成项”);
this.parentNode.append(a);
$.ajax({
类型:“POST”,
网址:“/”,
数据类型:“json”,
数据:{user_id:_st.a,关键字:val},
成功:功能(产品){
if(产品长度){
产品.forEach(功能(产品,索引){
b=document.createElement(“div”);
var div_class=“bundle_autocomplete_item”+索引;
b、 setAttribute(“类”,div_类);
b、 innerHTML=''+product.title+'';
$(b).在(单击)上,函数(e){
closeAllList();
window.location.href='https://'+Shopify.shop+'/products/'+product.handle;
});
a、 儿童(b);
});
}
}
});
});
输入打开(“按键关闭”,功能(e){
var x=document.getElementById(this.id+“自动完成列表”);
如果(x)x=x.getElementsByTagName(“div”);
如果(e.keyCode==40){
currentFocus++;
addActive(x);
}如果(e.keyCode==38){//up
当前焦点--;
addActive(x);
}否则如果(e.keyCode==13){
e、 预防默认值();
如果(当前焦点>-1){
如果(x)x[currentFocus]。单击();
}
}
});
函数addActive(x){
如果(!x)返回false;
清除活性(x);
如果(currentFocus>=x.length)currentFocus=0;
如果(currentFocus<0)currentFocus=(x.length-1);
x[currentFocus].classList.add(“自动完成活动”);
}
函数removeActive(x){
对于(变量i=0;i
出于个人原因,我从ajax调用中删除了url我觉得您的元素不可正确单击…类似于此,但我找不到修复方法,因为它看起来像ajax。正如@NiettheDarkAbsol所说,该元素可能不可单击。它可能比页面上的内容或其他内容小Aight谢谢大家,伊玛试着多看一点,用jsbin或JSFIDLE做一个简单可行的例子