想要javascript中$ bhai的价值 [英] Want value of $bhai in javascript
问题描述
#!/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
此代码基本上显示目录中的文件夹列表,并且由于href属性,所有目录都变为链接。如果我点击该链接,它应该打开该目录并显示所选目录的文件夹。
此代码显示唯一的第一个目录,如果我点击任何链接,alert只会给我第一个目录的名称。
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;
我是新手,请帮助我。 advance
I am new in this, please help me in this.
Thanks in advance
推荐答案
原因是因为 document.getElementById
元素匹配 id
。
尝试更改您的代码,如下所示:
Try changing your code like this:
onclick = myFunction(this)
- 您可以在这里使用 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);
}
请注意,这被认为是不好的做法,如果您使用非 id
属性的独特值。正如名称所示,它应该是识别属性,并且是整个文档的唯一属性。
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.
这篇关于想要javascript中$ bhai的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!