使用javascript为图片分配链接
Assigning a link to an image using javascript
我有一种使用第三方软件从PDF
创建交互式Web APP
的技术。
有可能在PDF
中加入hotspots links
,转化为Flash SWF desktop version app
?
也有一个移动版本,生成单独的图像,并失去任何链接。通过引用一些javascript,它生成了这个我不能直接更改的html。
我的目标是让例如/files/mobile/2.jpg
和/files/mobile/8.jpg
链接到我选择的一些地址。
由于我无法控制进入这个HTML
,是否存在一种方法来添加javascript
使某些图像基于它们的名称链接?
<div id="fbBookPages" class="fbBookPages">
<div class="fbPage" style="display: none; height: 1121px; width: 1121px; margin-left: 364px; z-index: 1;"><div class="fbPageLoading" style="display: none;"></div><img src="../files/mobile/1.jpg" style="width: 1121px; height: 1121px;"></div>
<div class="fbPage" style="display: none; height: 1121px; width: 1121px; margin-left: 364px; z-index: 1;"><div class="fbPageLoading" style="display: none;"></div><img src="../files/mobile/2.jpg" style="width: 1121px; height: 1121px;"></div>
<div class="fbPage" style="display: none; height: 1121px; width: 1121px; margin-left: 364px; z-index: 1;"><div class="fbPageLoading" style="display: none;"></div><img src="../files/mobile/3.jpg" style="width: 1121px; height: 1121px;"></div>
<div class="fbPage" style="display: none; height: 1121px; width: 1121px; margin-left: 364px; z-index: 1;"><div class="fbPageLoading" style="display: none;"></div><img src="../files/mobile/4.jpg" style="width: 1121px; height: 1121px;"></div>
<div class="fbPage" style="display: none; height: 1121px; width: 1121px; margin-left: 364px; z-index: 1;"><div class="fbPageLoading" style="display: none;"></div><img src="../files/mobile/5.jpg" style="width: 1121px; height: 1121px;"></div>
<div class="fbPage" style="display: none; height: 1121px; width: 1121px; margin-left: 364px; z-index: 1;"><div class="fbPageLoading" style="display: none;"></div><img src="../files/mobile/6.jpg" style="width: 1121px; height: 1121px;"></div>
<div class="fbPage" style="display: none; height: 1121px; width: 1121px; margin-left: 364px; z-index: 1;"><div class="fbPageLoading" style="display: none;"></div><img src="../files/mobile/7.jpg" style="width: 1121px; height: 1121px;"></div>
<div class="fbPage" style="display: none; height: 1121px; width: 1121px; margin-left: 364px; z-index: 1;"><div class="fbPageLoading" style="display: none;"></div><img src="../files/mobile/8.jpg" style="width: 1121px; height: 1121px;"></div>
<div class="fbPage" style="display: block; height: 1121px; width: 1121px; margin-left: 364px; z-index: 999;"><div class="fbPageLoading" style="display: none;"></div><img src="../files/mobile/9.jpg" style="width: 1121px; height: 1121px;"></div></div>
</div>
你完全可以在JavaScript中做到这一点,而且你不需要任何库:
function createImgLink(imgURL, linkURL) {
// Select Image from DOM Tree
var img = document.querySelector("[src='" + imgURL + "']");
// Create a new Link (<a>) DOM Node
var link = document.createElement("a");
link.href = linkURL;
// Insert newly created Link to the DOM Tree
img.parentNode.insertBefore(link, img);
// Move Image inside the Link
link.appendChild(img);
}
的例子:要将"http://google.com"链接添加到"../files/mobile/1.jpg"图像,您可以使用如下函数:
createImgLink("../files/mobile/1.jpg", "http://google.com");
JQuery:
var url = ... // build your url here
var theNewLink = $('<a href="' + url + '"></a>'); // here you create a link from nowhere
var yourImageContainer = $(".fbPage") ... or something like that, depending on your context; // select one image container
var yourImage = yourImageContainer.find("img"); // select the image of this container
theNewLink.append(yourImage); // move the image into the new link
yourImageContainer.append(theNewLink); // add the new link to the container
使用图片作为链接要使用图像作为链接,只需将<img>
标记嵌套在<a>
标记中:
An image as a link: <a href="http://www.yourlink.se">
<img border="0" alt="text" src="logo.gif" width="100" height="100">
</a>
相关文章:
- 如何在jQuery中将链接函数分配给变量
- 如何通过分配单个变量来减少此脚本中的链接数
- 将内部链接分配给视差网站的不同部分
- 保存链接分配值以及转到其他页面
- 在visualforce中为更新字段分配HTML链接/按钮
- 如何分配数据切换=“;下拉菜单”;用于wordpress中的父链接
- 如何为类元素分配链接
- 如何将活动类分配给与号.js中的当前路由链接
- 检查页面标题是否包含在链接属性中,并使用jQuery分配活动类
- 如何将变量分配给链接中包含的信息?(Javascript/HTML)
- AngularJS无法在链接函数内分配范围变量
- 在 JavaScript 中分配链接和类
- jQuery:使用 var 快捷方式分配数据以及如何链接功能以在加载和单击时运行
- 如何为链接分配随机颜色
- 如何通过分配window.location在历史记录中留下链接
- 如何为数组分配链接
- 分配一个页面(我不拥有)一个不同的名称,当链接被点击(或打开时)
- 如何分配javascript变量与链接标签的id之前去链接
- 文本分配给文本框,单击超链接
- 使用javascript为图片分配链接