Want value of $bhai in javascript

2019-02-19 21:47发布

#!/bin/sh

echo "Content-type: text/html"
echo ""
echo '<html>'
echo '<head>'
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
echo '</head>'
echo '<body><center><br><h3 align='center'>STATUS</h3></br></center>'
list=$(ls -l /tmp | grep "^d" | awk -F" " '{print $9}')
list1=$(echo $list | wc -w)
i=1
while [ $i -le $list1 ]
do
    bhai=$(echo $list | cut -d' ' -f$i)
    echo '<a href="#" onclick="myFunction(); return false;" id="movie" style="font-size: 30px; text-decoration: none; margin-left: 1cm">'$bhai'</a></font><br>'
    i=$((i+1))
done
echo '
    <script type="text/javascript">
        function myFunction () {
            var mtype = document.getElementById("movie").text;
        alert("Hi");
        alert(mtype);
        }
    </script>'
echo '</body></html>'
exit 0

This code basically displays the folders list in the directory and because of href attribute all directories becomes a link. If I click on that link it should open that directory and display folders of selected directory.
This code displays the only first directory, if I click on any link, alert gives me name of first directory only.

 var mtype = document.getElementById("movie").text;

I am new in this, please help me in this.
Thanks in advance

1条回答
Juvenile、少年°
2楼-- · 2019-02-19 22:12

The reason for that is because document.getElementById returns first element matching that id.

Try changing your code like this:

onclick=myFunction(this) - you can use this here, it will reference the <a/> element being clicked

function myFunction(movieElement) { 
    var mtype = movieElement.text;
    alert(mtype);
}

Please also note, that it's considered to be bad practice, if you use non-unique values for id attribute. As the name indicates it should be the identifying attribute and unique for the whole document.

查看更多
登录 后发表回答