在onclick图像更改后添加超链接到新图像

时间:2016-08-08 04:51:45

标签: javascript html

我尝试在使用onclick功能更改图像后向图像添加超链接。 onclick图像更改有效,但在更改图像时添加链接很困难。

<div class="masonary16 ms mblogvid"><img id="ms16" src="Images/img1.jpg" 
height="100%" onclick="overimg(); imglink();"/></div>



function overimg() {
document.getElementById("ms16").src = "Images/OVER/img2.jpg"; 
}

function imglink() {    
var parentEl = document.getElementById("ms16").parent;
var imgEl = parentEl.innerHtml;
parentEl.innerHtml = '<a href="http://www.google.com">' + imgEl + '</a>';}

感谢您的回复。

3 个答案:

答案 0 :(得分:0)

创建一个元素{ "tags": { "allowUnknownTags": true, "dictionaries": ["jsdoc","closure"] }, "opts": { "template": "pathTo/myTemplate", "destination": "pathTo/myJScriptDocs", "recurse": true }, "source": { "includePattern": ".+\\.js(doc)?$", "excludePattern": "(^|\\/|\\\\)_", "include": ["pathTo/myJSSources", "pathTo/README.md"] }, "plugins": [], "templates": { "cleverLinks": false, "monospaceLinks": false } } ,设置其链接(url),使用select clientNo,clientName,clientCity, RoomNo as "",DateTo as "", DateFrom as "" from Table1 union select clientNo,clientName as "",clientCity as "",RoomNo,DateTo,DateFrom from Table2 方法将图像包装在其中,然后将其包装在<a>内。

appendChild()

答案 1 :(得分:0)

尝试将父div作为href。那么我认为你可以解决问题。这是我的选择

<a href="" class="masonary16 ms mblogvid">
 <img id="ms16" src="Images/scoge-menswear-16.jpg" height="100%"onclick="overimg(); imglink();"/>
</a>

function imglink() {    
    var parentEl = document.getElementById("ms16").parent;
    parentEl.href="xyz.php"; 
   return false;;
}

答案 2 :(得分:0)

//process here and and return json to shown result on same page using popup/alert. return this.Json(new { Result = data.ToList() }, JsonRequestBehavior.AllowGet); 不是parentElement,但此功能只能显示我认为的html。因此无法使用parent。但是我们可以通过classList获取类名,所以拿一个类并抓取html就可以了......

.innerHTML