admin管理员组文章数量:1392007
I have a JavaScript variable imgIndex
that I'd like to send to an <a href="...">
so I can change the id based on imgIndex
Javascript:
<script type="text/javascript">
var imgArray = [<?php echo implode(',', getImages($cat, $site)) ?>];
window.imgIndex = <?php echo $imgid ?>;
$(document).ready(function() {
var img = document.getElementById("showimg");
img.src = imgArray[<?php echo $imgid ?>];
$(document).keydown(function (e) {
var key = e.which;
var rightarrow = 39;
var leftarrow = 37;
var random = 82;
if (key == rightarrow)
{
imgIndex++;
if (imgIndex > imgArray.length-1)
{
imgIndex = 0;
}
img.src = imgArray[imgIndex];
}
if (key == leftarrow)
{
if (imgIndex == 0)
{
imgIndex = imgArray.length;
}
img.src = imgArray[--imgIndex];
}
if (key == random)
{
imgIndex = Math.floor((Math.random()*(imgArray.length-1))+1);
img.src = imgArray[imgIndex];
}
});
});
</script>
HTML:
Then send imgIndex
here so the id of the image will equal imgIndex
:
<a href="?action=viewic&id=imgIndex"><img id="prevkey" src="./images/SiteDesign/prev.png" alt="Comic Navigation" /></a>
But it doesn't work.
Any suggestions on how to pass it to <a href="...">
?
EDIT:
The reason I can't use is because I need to manipulate a javascript variable, not a number (as the browser renders the php value).
I have a JavaScript variable imgIndex
that I'd like to send to an <a href="...">
so I can change the id based on imgIndex
Javascript:
<script type="text/javascript">
var imgArray = [<?php echo implode(',', getImages($cat, $site)) ?>];
window.imgIndex = <?php echo $imgid ?>;
$(document).ready(function() {
var img = document.getElementById("showimg");
img.src = imgArray[<?php echo $imgid ?>];
$(document).keydown(function (e) {
var key = e.which;
var rightarrow = 39;
var leftarrow = 37;
var random = 82;
if (key == rightarrow)
{
imgIndex++;
if (imgIndex > imgArray.length-1)
{
imgIndex = 0;
}
img.src = imgArray[imgIndex];
}
if (key == leftarrow)
{
if (imgIndex == 0)
{
imgIndex = imgArray.length;
}
img.src = imgArray[--imgIndex];
}
if (key == random)
{
imgIndex = Math.floor((Math.random()*(imgArray.length-1))+1);
img.src = imgArray[imgIndex];
}
});
});
</script>
HTML:
Then send imgIndex
here so the id of the image will equal imgIndex
:
<a href="?action=viewic&id=imgIndex"><img id="prevkey" src="./images/SiteDesign/prev.png" alt="Comic Navigation" /></a>
But it doesn't work.
Any suggestions on how to pass it to <a href="...">
?
EDIT:
The reason I can't use is because I need to manipulate a javascript variable, not a number (as the browser renders the php value).
Share Improve this question edited Nov 15, 2012 at 1:57 user3871 asked Nov 14, 2012 at 23:33 user3871user3871 12.7k36 gold badges140 silver badges283 bronze badges 3- 3 your question isn't clear. you should do your homework before posting a question. – Sourabh Commented Nov 14, 2012 at 23:37
-
2
you will need to show the code where the anchor
<a>
is generated. – cillierscharl Commented Nov 14, 2012 at 23:38 -
1
Couldn't you just move the
<?php echo $imgid ?>
into thehref
of the<a>
tag? – Cᴏʀʏ Commented Nov 14, 2012 at 23:42
3 Answers
Reset to default 3You can manipulate the target of the link after you are done processing imgIndex
.
document.getElementById("yourLinkId").href = "?action=viewic&id=" + window.imgIndex;
Other than that, you can append an event handler to the link, which I am showing here as a sample for jQuery:
$("#yourLinkId").on("click", function()
{
$(this).attr("href", "?action=viewic&id=" + window.imgIndex);
});
You can do it this way, if you echo imgIndex
into the <a>
tag like so:
<a href="?action=viewic&id=<?php echo $imgid ?>"><img id="prevkey" src="./images/SiteDesign/prev.png" alt="Comic Navigation" /></a>
Then you can get the imgid from the querystring with:
<?php $imgid = $_GET["id"] ?>
You can not use the php variable in link? if you can, is the best solution.
<a href="?action=viewic&id=<?php echo $imgid; ?>"><img id="prevkey" src="./images/SiteDesign/prev.png" alt="Comic Navigation"/></a>
本文标签: Pass javascript value into html lta hrefgtStack Overflow
版权声明:本文标题:Pass javascript value into html <a href> - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744768651a2624200.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论