简体   繁体   English

如何在锚标记中使用onclcik和href并仅使onclick有效。href仅适用于Google抓取

[英]How to use onclcik and href in anchor tag and make only onclick work.href is only for google crawling

In my website when i click on images it opens new page using onclcik openpopup(user defined function) 在我的网站上,当我单击图像时,它将使用onclcik openpopup(用户定义的函数)打开新页面

my problem is google will not index onclcik urls,it only index href urls. 我的问题是Google不会索引onclcik网址,它只会索引href网址。

So i want to use use onclcik and href in anchor tag and make only onclick work.href is only for Google crawling purpose only my code is as below 所以我想在锚标记中使用onclcik和href并仅使onclick工作。href仅用于Google爬行目的,仅我的代码如下

var imgurl = "http:"+lcurl+touch_id+".jpg";
                 var data = "<div id=charm_"+touch_id+" data-charmid="+touch_id+" data-vis="+bgid+" data-vid="+seid+" data-vtitle="+vtitle_url+" data-title="+title_url+" style='float:left;' class='mdl-cell mdl-cell--4-col charm-wrapper'>\n\
                <div class='mdl-card-wrapper charm-inner'>\n\
                  <div class='demo-card-wide mdl-card mdl-shadow--8dp'>\n\
                        <div class='mdl-card__media'>\n\
                        "+locicon+" \n\
                      <a href="popupurl" id='myAnchor' > \n\
                      <div style='position:relative;'>\n\
                      <div style='position:absolute;z-index:9;right:0px;bottom:0px;'><img width=32px src="+cf_assets+"img/hanger.png></div>\n\
                       <div id=imagearea_"+touch_id+" title='"+title+"' class='imagearea loading'>\n\
                      </div></div></a>\n\
                    </div>\n\
                          <div class='mdl-card__title'>\n\
                    <div class='right-charm'>\n\
                     <div class='social-share'>\n\
                     "+copycontent+"\n\
                     "+watsapp+"\n\
                          <a onclick=sharetofb('"+ct_domain+socialshare_url+"'); title='Facebook' id=fb"+touch_id+" \n\
                  class='fb_share'></a>\n\
                <a onclick=sharetotwitter('"+ct_domain+socialshare_url+"'); title='Twitter' id=tw"+touch_id+" class='tw_share'> </a>\n\
                          </div>\n\
                          </div>\n\
                          <div class='icharm'>\n\
                      <h2 class='mdl-card__title-text'>"+subtitle+"</h2>\n\
                      <div class='mdl-card__subtitle-text'>"+title+"</div>\n\
                      <div id=vid_"+touch_id+" class='mdl-card__subtitle-videoname'>"+videoname+"</div>\n\
                       </div>\n\
                        </div>\n\
                      </div>\n\
                  </div>\n\
                </div>";
      $('#main-home-page-inner').add(HTML(data));


          callcloudinary(imgurl,title,touch_id);
          callmasonry();

      if(i == setarr.length-1){


         localStorage.touchids=touchid_arr.join(",");
        localStorage.searchresults=gcharmarr.join(",");
     }
     // document.getElementById("main-home-page-inner").innerHTML += html;

    }

    /* document.getElementById("fb"+touch_id).setAttribute("onclick","sharetofb('"+title+"','"+text+"','"+ct_domain+socialshare_url+"','"+social_domain+lcurl+touch_id+".jpg')");*/
  }
   //addEventListener('click', function (ev) {
       //ev.preventDefault();
       // alert(ev.target.getAttribute("data-charmid"));
     // openpopup(ev.target.getAttribute("data-url"));
     // sendpym(ev.target.getAttribute("data-title"));
   // });
            $('myAnchor').on('click',function(ev){ 
 ev.preventDefault();
 openpopup(ev.target.getAttribute("popupurl"));
      sendpym(ev.target.getAttribute("data-charmid"));

}); });

           })
           .error(function(status, statusText, responseText) {
                //console.log(statusText);
                //console.log(responseText);
           });

Fix your code. 修正您的代码。 Place the elements in quotes. 将元素放在引号中。 Work correctly with quotes. 正确使用引号。

    var imgurl = "http:"+lcurl+touch_id+".jpg";
    var data = "<div id='charm_"+touch_id+"' data-charmid='"+touch_id+"' data-vis='"+bgid+"' data-vid='"+seid+"' data-vtitle='"+vtitle_url+"' data-title='"+title_url+"' style='float:left;' class='mdl-cell mdl-cell--4-col charm-wrapper'><div class='mdl-card-wrapper charm-inner'><div class='demo-card-wide mdl-card mdl-shadow--8dp'><div class='mdl-card__media'>"+locicon+"<a href='"+popupurl+"' id='myAnchor' ><div style='position:relative;'><div style='position:absolute;z-index:9;right:0px;bottom:0px;'><img width='32px' src='"+cf_assets+"img/hanger.png'></div><div id='imagearea_"+touch_id+"' title='"+title+"' class='imagearea loading'></div></div></a></div><div class='mdl-card__title'><div class='right-charm'><div class='social-share'>"+copycontent+""+watsapp+"<a onclick='sharetofb(\'"+ct_domain+socialshare_url+"\');' title='Facebook' id='fb"+touch_id+"' class='fb_share'></a><a onclick='sharetotwitter(\'"+ct_domain+socialshare_url+"\');' title='Twitter' id='tw"+touch_id+"' class='tw_share'></a></div></div><div class='icharm'><h2 class='mdl-card__title-text'>"+subtitle+"</h2><div class='mdl-card__subtitle-text'>"+title+"</div><div id='vid_"+touch_id+"' class='mdl-card__subtitle-videoname'>"+videoname+"</div></div></div></div></div></div>";

    $('#main-home-page-inner').add(HTML(data));
    callcloudinary(imgurl,title,touch_id);
    callmasonry();

    if(i == setarr.length-1)
    {
        localStorage.touchids=touchid_arr.join(",");
        localStorage.searchresults=gcharmarr.join(",");
    }
    }
} // WHY????

$('myAnchor').on('click',function(ev){ 
ev.preventDefault();
openpopup(ev.target.getAttribute("popupurl"));
sendpym(ev.target.getAttribute("data-charmid"));

See how to properly use the quotes ' and " in a variable in w3schools.com - JavaScript Strings 了解如何正确使用引号'"在一个变量w3schools.com -的JavaScript字符串

You can easily do this by returning false from the handler function at the end. 您可以通过最后从处理程序函数返回false来轻松实现此目的。

Refer to this post for more details 请参阅此帖子以获取更多详细信息

Also, you can use something like this if you are using JQuery: 另外,如果您使用的是JQuery,则可以使用如下所示的内容:

$('a').on('click',function(e){ 
     e.preventDefault();
     // Do your other stuff.
});

In this example I am targeting your anchor tag by tagname. 在此示例中,我按标记名定位您的锚标记。 Add a class or ID instead. 而是添加一个类或ID。

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

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