使用javascript为图像指定链接

时间:2016-09-07 09:18:57

标签: javascript html

我使用第三方软件从Web APP创建交互式PDF

可以在hotspots links中添加PDF转换为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>

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您绝对可以在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);
}

实施例: 添加&#34; http://google.com&#34;链接到&#34; ../ files / mobile / 1.jpg&#34;你可以使用这样的函数:

createImgLink("../files/mobile/1.jpg", "http://google.com");

答案 2 :(得分:-1)

使用图像作为链接  要将图片用作链接,只需将<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>