我已经尝试了一切来找到这个答案,现在是凌晨 1:00,我必须上床睡觉并放弃:(在我妻子为我提供第二次离婚之前!:)
简而言之,我的问题是,我使用谷歌 API(本地搜索、图像搜索和网络搜索)来定位我合并到我的网站中的各种字符串。它们被加载到动态 DIV 中,并且每组结果都被赋予一个 div,其中包含与其数字相关的整数。例如,
for (var i = 0; i < localSearch.results.length; i++) {
// Create HTML elements for search results
var letsseetitle = localSearch.results[i].titleNoFormatting;
letsseestreet = localSearch.results[i].streetAddress;
var letsseecity = localSearch.results[i].city;
var letsseeregion = localSearch.results[i].region;
var letsseecountr = localSearch.results[i].country;
var letsseestaticmapurl = localSearch.results[i].staticMapUrl;
var latresult = localSearch.results[i].lat;
var lng = localSearch.results[i].lng;
dv = document.createElement('div'); // create dynamically div tag
var attrname = "selectionbox" + i;
dv.setAttribute('id',attrname); //give id to it
var goFetchThis = letsseetitle + " " + letsseestreet + " " + letsseeregion + " " + letsseecountr;
dv.style.border="solid";
dv.style.padding="10px";
dv.style.backgroundColor="#80C4FF";
dv.title="Click to review this company.";
dv.innerHTML= letsseetitle + "<BR>" + letsseestreet + "<BR>" + letsseecity + "<BR>" + letsseeregion + "<BR>" + letsseecountr + "<BR>";
//var el = document.getElementById(attrname);
LoopIn = i;
// attach event onmouseclick to the created div tag
if(isIE){
dv.onclick = new Function("SelectThis(attrname, attrname)");
}
else{
dv.setAttribute("onclick", "SelectThis()");
}
document.getElementById("foundresultsdiv").appendChild( dv );
}
现在的问题是这样的。在 chrome 中,即 safari 和其他浏览器中,点击组件都没有问题。IE,当用户单击动态创建的 div 之一时,我的脚本会剥离内容并将它们加载到准备好用户提交的表单中。
执行此操作的脚本如下;
function SelectThis(e) {
var targ;
if (!e) var e = window.event;
if (!e) var e = elTags[e].getAttribute("name");
if (e.target) targ = e.target;
else if (e.srcElement) targ = e.srcElement;
if (!e) var e = window.event;
if (e.target) targ = e.target;
else if (e.srcElement) targ = e.srcElement;
if (targ.nodeType == 3) // defeat Safari bug
targ = targ.parentNode;
theId=targ.id
var TheStuffInside = document.getElementById(theId).innerHTML;
var linebyline = TheStuffInside.split("<br>");
var searchname = document.getElementById('searchname')
searchname.value = linebyline[0];
var stAddress = document.getElementById('stadd')
stAddress.value = linebyline[1];
var suburb = document.getElementById('suburb')
suburb.value = linebyline[2];
var stateLocal = document.getElementById('statelocal')
stateLocal.value = linebyline[3];
var country = document.getElementById('country')
country.value = linebyline[4];
}
现在我不能在这里吹牛,因为我根本没有写这个脚本,而且总的来说它运行良好。除了火狐。当我单击 Firefox 中的一个动态 div 时,什么都没有发生。事实上,该元素返回一个未定义的结果。从更改代码到在加载时添加事件侦听器,我尝试了很多变化,但我根本没有运气。
这里的任何建议将不胜感激。谁知道——你甚至可以挽救我的婚姻!:)
顺便说一句,//beat safari 错误在技术上是不正确的,但我没写好!:)
有任何问题请告诉我。