Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不改变dom的Javascript页面覆盖_Javascript_Jquery - Fatal编程技术网

不改变dom的Javascript页面覆盖

不改变dom的Javascript页面覆盖,javascript,jquery,Javascript,Jquery,我正在学习JS,并且正在尝试向我页面上的所有链接添加一个整数(点击次数)(这些数据是从外部API解析的) 我目前正在更改每个链接的链接textContent,并将整数添加到textContent的末尾,这对所有文本链接都很有效,但是我在将这个click count整数添加到页面上的图像时遇到了问题。在页面上显示图像点击次数的最佳方式是什么。我能否将点击次数添加为图像上的覆盖 到目前为止,我的代码是: function ls(url) { var getURL = "url" + url;

我正在学习JS,并且正在尝试向我页面上的所有链接添加一个整数(点击次数)(这些数据是从外部API解析的)

我目前正在更改每个链接的链接textContent,并将整数添加到textContent的末尾,这对所有文本链接都很有效,但是我在将这个click count整数添加到页面上的图像时遇到了问题。在页面上显示图像点击次数的最佳方式是什么。我能否将点击次数添加为图像上的覆盖

到目前为止,我的代码是:

function ls(url) {
  var getURL = "url" + url;
  var req = new XMLHttpRequest();
  req.open("GET", getURL, "true");
  req.onload = function() {
    var resObj = JSON.parse(req.responseText);
    var links = document.querySelectorAll("a");
    for (var i = 0; i < links.length; i++) {
      var rawLink = links[i].href; var linkText = links[i].textContent; var link = links[i].href.replace(/(.*)#?/, "$1");
      var escapedLink = escape(rawLink);
      if (rawLink in resObj) {
        links[i].textContent = linkText + " (" + resObj[rawLink] + ")";
      } else if (escapedLink in resObj) {
        links[i].textContent = linkText + " (" + resObj[escapedLink] + ")";
      }
    }
  };
函数ls(url){
var getURL=“url”+url;
var req=新的XMLHttpRequest();
请求打开(“GET”,getURL,“true”);
req.onload=函数(){
var resObj=JSON.parse(req.responseText);
var links=document.querySelectorAll(“a”);
对于(变量i=0;i

这只是将链接计数附加到文本内容的末尾,如何在不破坏页面布局的情况下将此链接计数添加到图像中。

一种解决方案是将图像包装在
中,并将单击计数文本添加为另一个子项。类似于:

<div class="image-wrapper">
    <img src="..." alt="..." />
    <div class="click-count">10</div>
</div>
.image-wrapper { position: relative; }
.click-count { position: absolute; bottom: 10px; right: 10px; }
要将文本添加到图像中,您必须执行以下操作(注意,为了简单起见,我使用jQuery,因为问题被标记为这样):

$('img')。每个(函数(){
//测试我们需要添加点击次数
...
//添加单击计数编号
$(this.wrap(“”).append(“”+单击计数+“”);
});

你的问题非常模糊。考虑提供一个你所拥有的代码示例,并以更好的方式描述你想要的。现在,答案是“可能”。在我完成之前错误地发布问题!刚刚更新。
$( 'img' ).each( function() {
    // Test we need to add the click count
    ...

    // Add the click count number
    $( this ).wrap( '<div class="image-wrapper" />' ).append( '<div class="click-count">' + click_count + '</div>' );
});