【问题标题】:Why aren't the links I'm adding to the DOM with Javascript clickable?为什么我使用 Javascript 添加到 DOM 的链接不可点击?
【发布时间】:2010-11-22 03:43:54
【问题描述】:

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

所以这里有几个步骤 - 我已经让部分功能正常工作。

这是 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 的理解而言,&lt;a&gt; 标签的全部内容应该是可点击的。

有谁知道问题出在哪里?

我想弄清楚的其他问题是如何在不需要用户点击的情况下自动查询图像。

一旦完成,我将尝试将整个事情变成一个事件侦听器,它会自动查询并在搜索窗口中的 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>

【问题讨论】:

    标签: javascript dom


    【解决方案1】:

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

    浏览器有不同的方式来处理这样的错误元素,但他们会尝试从中找出某种意义。处理错误的一种方法是将块元素移到内联元素之外,这当然意味着它不可点击。

    使用 span 元素代替 div 元素,然后您可以使用 CSS 样式将链接和 span 元素都变成块元素。

    【讨论】:

    • 奇怪的是,如果我将图像移动到 A 元素内唯一的东西,图像是可点击的……但前提是我不将 styleFloat 应用于包含它们的 DIV。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 2015-11-28
    • 2019-03-28
    相关资源
    最近更新 更多