English 中文(简体)
无法使用 DOM 分析点击文本
原标题:Unable to parse onClick text using DOM

我所读到的都说要使用 element 属性, 但在我的状态下似乎没有效果。 我试图解析数字 : 629216818, 并将其设置为 varialbe: fbid 。 这是一个 Greasemonkey 脚本, 这样 HTML 无法直接编辑 。 我不行, 所以我可能只是做一些愚蠢的事情, 但这里是我的 HTML 和 Javascript :

<div id="petRightContainer">
<a title = "Pet trainer bonus: Your companion will level 5% faster." href="setup.php?type=companion&gtRandom=8167343321487308">
<div class="petRight" style="background-image:url(/fb/res/gui4/companion/cu_sith.jpg)"></div>
</a>
<div class="petRightLevel">
<a href="#" onClick="publishToWall( http://www.ghost-trappers.com/fb ,  Look at the cool augmentations my companion has received on Ghost Trappers. ,  Look at my new companion on Ghost Trappers!  I ve named it Jankie.   , null,  index.php?si=629216818&fromWall=1 ,  white/companion_32.jpg ,  companion/wallpost_augmentation_12.jpg ,  companion/wallpost_augmentation_21.jpg ,  companion/wallpost_augmentation_11.jpg , null)">Dog</a>                        
</div>

<script type="text/javascript">

    fbid = 0;
    fbidRegex = /d{3,}(?=&fromWall=1)/;

    if ( document.getElementsByClassName("petRightLevel")[0]){

       element = document.getElementsByClassName("petRightLevel")[0].firstChild;
       codeStore = element.onclick;
       fbid = fbidRegex.exec(codeStore);
       document.write("it is working ");
    }

    document.write(fbid);

</script>
最佳回答

问题就在这行:

element = document.getElementsByClassName("petRightLevel")[0].firstChild;

如果您使用 Firefox 和其他支持 文件的浏览器。 getEeminlesByClassName 并在您的 HTML 中, 在 和 HTML 之间有空格。

<a href="#" onClick= ...>

,第一个“子”实际上是文本节点,而不是链接。你只需要删除这两个元素之间的空格和(或)断线。

如果您正在使用 IE, 问题仍然与 javascript 相同, 因为 IE 不支持文档。 getElementsByClassName 到版本 8 。

<强度> 更新: 为所有我测试过的浏览器在不触动 HTML 的情况下完成的下列 javacrip 代码工作 :

<script type="text/javascript">

    fbid = 0;
    fbidRegex = /d{3,}(?=&fromWall=1)/;

    var divs = document.getElementsByTagName("div");
    var link = null;

    for (var i=0;i<divs.length;i++)
    {
       if(divs[i].getAttribute("class") ==="petRightLevel")
       {
          link = divs[i].getElementsByTagName("a")[0];
          break;
       }
    }
    if (link){
       codeStore = link.onclick;
       fbid = fbidRegex.exec(codeStore);
       document.write("it is working ");
    }

    document.write(fbid);

</script>

如果你只需要得到锚, 它会比这个简单得多。

问题回答

我想这对你可能有用

<script type="text/javascript">
    fbid = 0;
    fbidRegex = /d{3,}(?=&fromWall=1)/;
    if(document.getElementsByClassName("petRightLevel")[0]){
        element = document.getElementsByClassName("petRightLevel")[0].firstChild;
        // callback function to execute when the element onclick event occurs.
        codeStore = element.onclick = function(){
            fbid = fbidRegex.exec(codeStore);
            document.write("it is working ");
            document.write(fbid);
        }
    }
</script>




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.