如何通过id从html元素中提取文本并将其分配给一个php变量? [英] how to extract text from a html element by id and assign to a php variable?

查看:304
本文介绍了如何通过id从html元素中提取文本并将其分配给一个php变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个:

 < h4 class =modal-titleid =exampleModalLabel>你好< / h4> 

我想用<$ c $提取 hello c> id 并将其分配给一个PHP变量,但我不知道。如果它是一个输入会更容易,但我有哟使用不同的元素

解决方案

您可以使用

  document.getElementById('exampleModalLabel')。innerHTML 

返回 hello



请参阅 mozilla.org/en-US/docs/Web/API/Element/innerHTMLrel =nofollow noreferrer> docs


Element.innerHTML属性设置或获取描述
元素后代的HTML语法。


要将其分配给php变量,请尝试

 <?php $ text =< script> document.writeln(document.getElementById(' 。exampleModalLabel')的innerHTML);< /脚本>中; 

UPDATE 在看到后如果是输入会更容易,但我有你的问题使用不同的元素



使用 document.getElementById('exampleModalLabel' ).innerHTML 来设置一个隐藏的输入字段的值,然后你可以在问题中使用它。


I have this:

<h4 class="modal-title" id="exampleModalLabel"> hello </h4>

and I want to extract the hello word using its id and assign this to a php var but I don't have idea. If it were an input would be easier but I have yo use a different element

解决方案

you can use

document.getElementById('exampleModalLabel').innerHTML

This returns hello.

See the fiddle

According to the docs

The Element.innerHTML property sets or gets the HTML syntax describing the element's descendants.

To assign this to a php variable, please try

<?php $text="<script>document.writeln(document.getElementById('exampleModalLabel').innerHTML);</script>";

UPDATE after seeing If it were an input would be easier but I have yo use a different element in your question

Use document.getElementById('exampleModalLabel').innerHTML to set the value of a hidden input field and then you can use it as you said in the question.

这篇关于如何通过id从html元素中提取文本并将其分配给一个php变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆