使用javascript在php代码中显示标记的值(Displaying value of tag in php code using javascript)
我最近开始学习CSS,我发现一个任务是我需要从这个PHP代码显示标签汽车的价值:
<html><head></head><body> <house></house> <star> </star> <car> </car> <car>aa test</car> <car>aa test</car> <car>aa test</car> <car>aa test</car> <car>aa test</car> <car>aa test</car> <car>aa test</car> <car>aa test</car> <car>aa test</car> <star> </star> <sun> </sun> <star> </star> <house></house> <car>test </car> <sun> </sun> <car>zly test</car> <car>zly test</car> <car>zly test</car> <car>zly test</car> <car>zly test</car> <car>zly test</car> <car>zly test</car> <car>zly test</car> <car>zly test</car> <script> alert(document.querySelectorAll("xxxxxxx")[0].innerHTML); </script> </body></html>关键是要打印此标记的值,仅更改以下命令中引号中的内容:
alert(document.querySelectorAll("xxxxxx")[0].innerHTML);我知道如何显示单个标签的值,但这里名为car的标签有几个不同的值。 有没有简单的方法来显示它?
I've started to learn CSS lately and I found a task were I need to display value of a tag car from this php code :
<html><head></head><body> <house></house> <star> </star> <car> </car> <car>aa test</car> <car>aa test</car> <car>aa test</car> <car>aa test</car> <car>aa test</car> <car>aa test</car> <car>aa test</car> <car>aa test</car> <car>aa test</car> <star> </star> <sun> </sun> <star> </star> <house></house> <car>test </car> <sun> </sun> <car>zly test</car> <car>zly test</car> <car>zly test</car> <car>zly test</car> <car>zly test</car> <car>zly test</car> <car>zly test</car> <car>zly test</car> <car>zly test</car> <script> alert(document.querySelectorAll("xxxxxxx")[0].innerHTML); </script> </body></html>The point is to print the value of this tag changing only content in the quotation marks in the following command :
alert(document.querySelectorAll("xxxxxx")[0].innerHTML);I know how to display the value of a single tag but here the tag named car has a few different values. Is there any simple method to display it?
最满意答案
您可以遍历querySelectorAll数组中的每个项:
var cars = document.querySelectorAll("car"); var allCars = ""; for (var i = 0; i < cars.length; i++) { allCars += cars[i].innerHTML; } alert(allCars);你可以在这里看到一个有效的JSFiddle。
You can iterate through each item in the querySelectorAll array:
var cars = document.querySelectorAll("car"); var allCars = ""; for (var i = 0; i < cars.length; i++) { allCars += cars[i].innerHTML; } alert(allCars);You can see a working JSFiddle here.
更多推荐
发布评论