Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.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
Javascript 图像的窗口位置href_Javascript_Jquery - Fatal编程技术网

Javascript 图像的窗口位置href

Javascript 图像的窗口位置href,javascript,jquery,Javascript,Jquery,尝试向图像添加共享按钮,当前javascript中的配置如下: url: window.location.href 我希望url是图像的url,而不是整个页面的url。这是配置 this.config = { enabled_networks: 0, protocol: ['http', 'https'].indexOf(window.location.href.split(':')[0]) === -1 ? 'https://' : '//', url: window.l

尝试向图像添加共享按钮,当前javascript中的配置如下:

url: window.location.href
我希望url是图像的url,而不是整个页面的url。这是配置

    this.config = {
  enabled_networks: 0,
  protocol: ['http', 'https'].indexOf(window.location.href.split(':')[0]) === -1 ? 'https://' : '//',
  url: window.location.href,
  caption: null,
  title: (content = document.querySelector('meta[property="og:title"]') || document.querySelector('meta[name="twitter:title"]')) ? content.getAttribute('content') : (content = document.querySelector('title')) ? content.innerText : void 0,
  image: (content = document.querySelector('meta[property="og:image"]') || document.querySelector('meta[name="twitter:image"]')) ? content.getAttribute('content') : void 0,
  description: (content = document.querySelector('meta[property="og:description"]') || document.querySelector('meta[name="twitter:description"]') || document.querySelector('meta[name="description"]')) ? content.getAttribute('content') : '',
有人能告诉我如何将图像作为url而不是整个页面的目标吗?

试试这个:

url: $("yourImageSelector").prop("src")
这将从DOM中获取图像,然后获取其
src
属性的值,即图像的URL(请注意,此方法还将为您将URL转换为绝对URL)

您的图像html代码:

  <img id="my_image" src="jquery.com/jquery-wp-content/themes/jquery/images/logo-jquery.png" />

什么形象?我在代码中没有看到任何提到图像的地方。@DOCTYPEHTML-首先,图像没有自己的
href
s,如果您单击它们,它们不会引导您到任何地方,除非您将它们包装在锚定标记中。第二,即使他们这样做了,他们的
onclick
属性也不会给出该位置的URL。@ZacharyKniebel我想是这样的
$(“#我的图像”).attr(“onclick”,“window.location=”“+hrefrink+”)
当您可以简单地执行
$(“#我的图像”)。在(“单击”,函数(){window.location=…})上,为什么要使用jQuery设置
onclick
属性?无论哪种方式,这都不是重点,因为这不是OP所要求的。document.images[0]。src attr()会转换相对URL吗?我知道prop()用于src,因为这是浏览器提供的,但是jq是否重新写入attrib值?如果是这样,那似乎是一件非常糟糕的事情……不,你是对的,@dandavis。那是我的错误。我会更新
.attr
以明文形式读取属性这个答案与我的答案有什么不同,除了你犯了我犯的错误,dandavis发现并纠正了我?我没有看到你的答案,抱歉,伙计
  this.config = {
     enabled_networks: 0,
     protocol: ['http', 'https'].indexOf(window.location.href.split(':')[0]) === -1 ? 'https://' : '//',
     url: $('#my_image').attr('src'),
     caption: null,
     title: (content = document.querySelector('meta[property="og:title"]') || document.querySelector('meta[name="twitter:title"]')) ? content.getAttribute('content') : (content = document.querySelector('title')) ? content.innerText : void 0,
     image: (content = document.querySelector('meta[property="og:image"]') || document.querySelector('meta[name="twitter:image"]')) ? content.getAttribute('content') : void 0,
     description: (content = document.querySelector('meta[property="og:description"]') || document.querySelector('meta[name="twitter:description"]') || document.querySelector('meta[name="description"]')) ? content.getAttribute('content') : '',