Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php I';我无法使用JavaScript设置图像_Php_Javascript_Image_Getelementbyid - Fatal编程技术网

Php I';我无法使用JavaScript设置图像

Php I';我无法使用JavaScript设置图像,php,javascript,image,getelementbyid,Php,Javascript,Image,Getelementbyid,我试图制作一个评级系统,但我站在javascript的立场上。我提出了一种方法,假设用一颗恒星替换一颗空白恒星的图像 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]).s

我试图制作一个评级系统,但我站在javascript的立场上。我提出了一种方法,假设用一颗恒星替换一颗空白恒星的图像

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";
}
函数setStar(intStarId){
var stars=新阵列(“star_1”、“star_2”、“star_3”、“star_4”、“star_5”);
对于(变量i=0;i
当我调用这个方法时,它对星号没有任何作用。我怎么才能得到这个工作,所以它把空白的恒星图像换成星图?

下面是我如何在html中调用它。(它实际上是php,但与html相呼应)

echo“setStar(3)”;
下面是星星的html(php呼应html)

echo“
”;
我认为应该使用getElement*sByName*而不是getElement*ById*;-) 请注意,因为getElementsByName不返回元素,而是返回节点列表:

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";

    }
}
函数setStar(intStarId){
var stars=新阵列(“star_1”、“star_2”、“star_3”、“star_4”、“star_5”);
对于(变量i=0;i
我认为应该使用getElement*sByName*而不是getElement*ById*;-) 请注意,因为getElementsByName不返回元素,而是返回节点列表:

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";

    }
}
函数setStar(intStarId){
var stars=新阵列(“star_1”、“star_2”、“star_3”、“star_4”、“star_5”);
对于(变量i=0;i
您正试图使用id在DOM中查找元素。但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 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>";
echo“
”;

还请注意,对于DOM中的所有元素,id都应该是唯一的。

您正在尝试使用id在DOM中查找元素。但是您的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 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>";
echo“
”;

还请注意,对于DOM中的所有元素,id都应该是唯一的。

您应该只使用jQuery,使用下面的库会更简单:)


查看一下,您应该只使用jQuery,使用以下库会更容易:)


看看和

或者在你的标记中用id替换名称(由Ramesh:-)或者在你的标记中用id替换名称(由Ramesh:-)我甚至没有注意到。非常感谢。我甚至没有注意到。非常感谢。