使用javascript在php代码中显示标记的值(Displaying value of tag in php code using javascript)

编程入门 行业动态 更新时间:2024-10-13 10:30:24
使用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.

更多推荐

本文发布于:2023-07-14 18:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106694.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标记   代码   php   javascript   tag

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!