I'm trying to make a ratings system but I'm at a stand with the javascript. I have made a method which is suppose to swap out the image of a blank star for a star.
function setStar(intStarId) {
var stars = new Array("star_one","star_two","star_three","star_four","star_five");
for (var i = 0; i < intStarId; i++)
document.getElementById(stars[i]).src = "images/star.png";
}
When I call this method it does not do anything with the star. How could I get this working so it swaps the blank star image for the star image?
Here is how I'm calling it in html. (it's actually php but it's echoing html)
echo "<script language='javascript'>setStar(3)</script>";
And here is the html for the stars (php echoing html)
echo "<ul name='a' style='list-style: none; margin: 0px; float: right;'>
<li name='b' align='center'>
<form name = 'test' action='#' method='post'>
<a href='#'><img name='star_one' src='images/star_blank.png' onmouseover='hoverInStar(1)' onmouseout='hoverOutStar(1)'/></a>
<a href='#'><img name='star_two' src='images/star_blank.png' onmouseover=开发者_如何学JAVA'hoverInStar(2)' onmouseout='hoverOutStar(2)'/></a>
<a href='#'><img name='star_three' src='images/star_blank.png' onmouseover='hoverInStar(3)' onmouseout='hoverOutStar(3)'/></a>
<a href='#'><img name='star_four' src='images/star_blank.png' onmouseover='hoverInStar(4)' onmouseout='hoverOutStar(4)'/></a>
<a href='#'><img name='star_five' src='images/star_blank.png' onmouseover='hoverInStar(5)' onmouseout='hoverOutStar(5)'/></a>
</form>
</li>
</ul>";
You are trying to find the element in DOM by using the id. But your HTML output contains only name. Try modifying you code to below
echo "<ul name='a' style='list-style: none; margin: 0px; float: right;'>
<li name='b' align='center'>
<form name = 'test' action='#' method='post'>
<a href='#'><img id='star_one' src='images/star_blank.png' onmouseover='hoverInStar(1)' onmouseout='hoverOutStar(1)'/></a>
<a href='#'><img id='star_two' src='images/star_blank.png' onmouseover='hoverInStar(2)' onmouseout='hoverOutStar(2)'/></a>
<a href='#'><img id='star_three' src='images/star_blank.png' onmouseover='hoverInStar(3)' onmouseout='hoverOutStar(3)'/></a>
<a href='#'><img id='star_four' src='images/star_blank.png' onmouseover='hoverInStar(4)' onmouseout='hoverOutStar(4)'/></a>
<a href='#'><img id='star_five' src='images/star_blank.png' onmouseover='hoverInStar(5)' onmouseout='hoverOutStar(5)'/></a>
</form>
</li>
</ul>";
Please also note, id should be unique for all the elements in the DOM.
I think you should use getElement*sByName* instead getElement*ById* ;-) Pay attention, because getElementsByName doesn't return an element, but a node list:
function setStar(intStarId) {
var stars = new Array("star_one","star_two","star_three","star_four","star_five");
for (var i = 0; i < intStarId; i++) {
document.getElementsByName(stars[i])[0].src = "images/star.png";
}
}
You should just use jQuery, with the following Library, it would have been easier :)
Have a look at http://www.jquery.com/ and http://plugins.learningjquery.com/half-star-rating/
精彩评论