将部分html标签复制到另一个

问题描述 投票:3回答:1

我的php页面中有一些图像,我想将其中一个复制到页面的<meta property="og:image" content="" />部分的head标签中。

图像标签示例:

<img class="img-responsive" border="0" src="../../../Gallery/path/file.jpg" alt="some text" width="640" height="460">

预期输出(将添加到该部分)修剪上述行中的所有内容,并从中获取图像的路径:

<meta property="og:image" content="../../../Gallery/path/file.jpg" />

我尝试了与getElementByIdinnerHTML一致的事情,但它不起作用。也像:jQuery( "img.img-responsive" );,(但它需要整行)。我认为它需要像.removeAttr()这样的东西,但我不确定如何一起实现所有这些。

  • 另一件事,我不确定它是否可行:如果页面有更多具有相同名称但具有不同图像的类,是否可以在jQuery或Javascript代码中指定要使用哪个图像? (例如为第一张图片添加,1,或者仅为第二张图片添加,2
javascript jquery html getelementbyid
1个回答
1
投票

看看attr() http://api.jquery.com/attr/eq() https://api.jquery.com/eq-selector/

$('meta[property="og:image"]').attr('content', $('.img-responsive:eq(2)').attr('src') ); // eq(2) for the 3rd image

© www.soinside.com 2019 - 2024. All rights reserved.