简体   繁体   English

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

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

I'm writing a Bookmarklet to change the behaviour of the google homepage. 我正在编写一个书签来更改Google主页的行为。 I love the visual search page preview functionality. 我喜欢视觉搜索页面预览功能。 Coolest.thing.ever. 最酷的东西。 BUT I'd really like to see what it'd be like if it worked as a LIVE SEARCH as opposed to having to mouseover the links to see it. 但是我真的很想看看如果它作为实时搜索工作,而不是将鼠标悬停在链接上才能看到它会是什么样子。

So there are a few steps to this - I've got partial functionality working. 因此,需要执行一些步骤-我已经完成了部分功能。

Here's the JS code. 这是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);
})();

The way it works at the moment is 目前它的工作方式是

  1. put the above code in a bookmark 将上面的代码放在书签中
  2. search for something on google 在Google上搜索内容
  3. click the magnifying glass 点击放大镜
  4. click the bookmark 点击书签

At the moment, for some reason - the links that get added to the page are not clickable unless there are text boxes on the image preview. 目前,由于某种原因-除非图像预览上有文本框,否则无法单击添加到页面的链接。 As far as my understanding of DOM goes, the whole content of the <a> tag should be clickable. 就我对DOM的理解而言, <a>标记的全部内容应可单击。

Anyone know what the problem is? 有人知道是什么问题吗?

Other questions I'd like to figure out is how to auto-query the images without requiring the user to click. 我想找出的其他问题是如何在不要求用户点击的情况下自动查询图像。

Once that's done I'll try to turn the whole thing into an event-listener which auto-queries and displays the images on keyup in the search window. 完成此操作后,我将尝试将整个事件变成事件侦听器,该事件侦听器将自动查询并在搜索窗口中的keyup上显示图像。

How cool will that be?! 那有多酷? :) :)

I can't seem to get the bookmarklet to "compile" here on stack but I've got one you can drag into your toolbar here: http://chesser.ca/2010/11/google-visual-image-search-hack-marklet . 我似乎无法在此处将小书签“编译”到堆栈中,但是我可以将其拖动到此处的工具栏中: http : //chesser.ca/2010/11/google-visual-image-search- hack-marklet

or the href should look like this 或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>

An inline element (a) can not contain a block element (div). 内联元素(a)不能包含块元素(div)。

Browsers have different ways of handing incorrect elements like this, but they will try to make some kind of sense out of it. 浏览器有不同的方式来处理这样的不正确元素,但是它们会尝试从中获得某种意义。 One way to handle the error is to move the block element outside the inline element, which of course means that it's not clickable. 处理错误的一种方法是将block元素移到inline元素之外,这当然意味着它不可单击。

Use span elements instead of div elements, then you can use CSS styles to chance both the link and the span elements into block elements. 使用span元素而不是div元素,然后可以使用CSS样式将链接和span元素同时设置为块元素。

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

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