繁体   English   中英

为什么我使用Javascript添加到DOM的链接不是可点击的?

[英]Why aren't the links I'm adding to the DOM with Javascript clickable?

我正在编写一个书签来更改Google主页的行为。 我喜欢视觉搜索页面预览功能。 最酷的东西。 但是我真的很想看看如果它作为实时搜索工作,而不是将鼠标悬停在链接上才能看到它会是什么样子。

因此,需要执行一些步骤-我已经完成了部分功能。

这是JS代码。

javascript: (function() {
    c = document.getElementById('ires');
    nli = document.createElement('div');

    cell = 0;
    for (var Obj in google.vs.ha) {
        na = document.createElement('a');
        na.href = Obj;
        na.style.cssFloat = 'left';
        na.style.styleFloat = 'left';

        nd = document.createElement('div');
        cldiv = document.createElement('div');
        cldiv.style.clear = 'both';
        nd.style.width = google.vs.ha[Obj].data.dim[0] + 'px';
        nd.style.height = google.vs.ha[Obj].data.dim[1] + 'px';
        nd.style.margin = '5px';
        nd.style.padding = '5px';
        nd.style.border = '1px solid #999999';


        if (google.vs.ha[Obj].data.tbts.length) {
            nilDiv = document.createElement('div');
            for (i = 0; i < google.vs.ha[Obj].data.tbts.length; i++) {
                box = google.vs.ha[Obj].data.tbts[i].box;
                newbox = document.createElement('div');
                newbox.className = 'vsb vsbb';
                newbox.style.position = 'relative';
                newbox.style.top = (box.t) + 'px';
                newbox.style.left = box.l + 'px';
                newbox.style.height = box.h + 'px';
                newbox.style.width = box.w + 'px';
                nilDiv.appendChild(newbox);
                newtext = document.createElement('div');
                newtext.className = 'vsb vstb';
                newtext.innerHTML = google.vs.ha[Obj].data.tbts[i].txt;
                newtext.style.top = (box.t) + 'px';
                newtext.style.position = 'relative';
                nilDiv.appendChild(newtext);
            }
            nilDiv.style.height = '0px';
            nd.appendChild(nilDiv);
        }

        for (i = 0; i < google.vs.ha[Obj].data.ssegs.length; i++) {
            ni = document.createElement('img');
            ni.src += google.vs.ha[Obj].data.ssegs[i];
            ni.className += ' vsi';
            nd.appendChild(ni);
        }
        na.appendChild(nd);
        nli.appendChild(na);
    };
    c.insertBefore(nli, c.firstChild);
})();

目前它的工作方式是

  1. 将上面的代码放在书签中
  2. 在Google上搜索内容
  3. 点击放大镜
  4. 点击书签

目前,由于某种原因-除非图像预览上有文本框,否则无法单击添加到页面的链接。 就我对DOM的理解而言, <a>标记的全部内容应可单击。

有人知道是什么问题吗?

我想找出的其他问题是如何在不要求用户点击的情况下自动查询图像。

完成此操作后,我将尝试将整个事件变成事件侦听器,该事件侦听器将自动查询并在搜索窗口中的keyup上显示图像。

那有多酷? :)

我似乎无法在此处将小书签“编译”到堆栈中,但是我可以将其拖动到此处的工具栏中: http : //chesser.ca/2010/11/google-visual-image-search- hack-marklet

或href应该看起来像这样

<a href="javascript:(function(){c=document.getElementById('ires');nli=document.createElement('div');cell=0;for(var Obj in google.vs.ha){na=document.createElement('a');na.href=Obj;na.style.cssFloat='left';na.style.styleFloat='left';nd=document.createElement('div');cldiv=document.createElement('div');cldiv.style.clear='both';nd.style.width=google.vs.ha[Obj].data.dim[0]+'px';nd.style.height=google.vs.ha[Obj].data.dim[1]+'px';nd.style.margin='5px';nd.style.padding='5px';nd.style.border='1px solid #999999';if(google.vs.ha[Obj].data.tbts.length){nilDiv=document.createElement('div');for(i=0;i<google.vs.ha[Obj].data.tbts.length;i++){box=google.vs.ha[Obj].data.tbts[i].box;newbox=document.createElement('div');newbox.className='vsb vsbb';newbox.style.position='relative';newbox.style.top=(box.t)+'px';newbox.style.left=box.l+'px';newbox.style.height=box.h+'px';newbox.style.width=box.w+'px';nilDiv.appendChild(newbox);newtext=document.createElement('div');newtext.className='vsb vstb';newtext.innerHTML=google.vs.ha[Obj].data.tbts[i].txt;newtext.style.top=(box.t)+'px';newtext.style.position='relative';nilDiv.appendChild(newtext);}nilDiv.style.height='0px';nd.appendChild(nilDiv);}for(i=0;i<google.vs.ha[Obj].data.ssegs.length;i++){ni=document.createElement('img');ni.src+=google.vs.ha[Obj].data.ssegs[i];ni.className+=' vsi';nd.appendChild(ni);}na.appendChild(nd);nli.appendChild(na);};c.insertBefore(nli,c.firstChild);})();">bookmarklet</a>

内联元素(a)不能包含块元素(div)。

浏览器有不同的方式来处理这样的不正确元素,但是它们会尝试从中获得某种意义。 处理错误的一种方法是将block元素移到inline元素之外,这当然意味着它不可单击。

使用span元素而不是div元素,然后可以使用CSS样式将链接和span元素同时设置为块元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM