如何在PHP中获取span的内部文本 [英] How to Get the inner text of a span in PHP

查看:530
本文介绍了如何在PHP中获取span的内部文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我已经创建了我自己的下拉框。我有一个Div,在div里我有一个跨度。我需要抓住内部的跨度文本。我怎么做?跨度的文本确实发生了变化。我15岁,请给我休息一下!我需要一些帮助!这是价值是纠察队。

 < div> 
< select id =test_selectstyle =display:none;>
< option value =1>平面设计师< / option>
< option value =2> Animator< / option>
< option value =7>播音员< /选项>
< option value =11>网站开发人员< / option>
< option value =12>判断< / option>
< / select>

< / div>

这就是需要得到的:

 < div class =custom-select> 
< span>选择选项< / span>
< ul>
< / ul>
< / div>


解决方案

好吧,您需要的不仅仅是PHP来完成这件事;您还需要JavaScript。



让我们从您的HTML开始。我假设你的渲染输出如下所示,我不会质疑你为什么这样做。

  < div id =dropdown> 
< span>选项一< / span>
< span>选项二< / span>
< span>选项三< / span>
< / div>

所以我猜你的HTML。



要将值发回PHP,您还需要一种方法来捕获可以使用表单发布的输入字段中的选定值。隐藏的输入可能是您的最佳选择。

 < input type =hiddenname =dropdown-selection id =dropdown-selection/> 

这就是我们的标记完成。接下来,您需要从div中获取所选选项并将其粘贴到隐藏字段中。我假设你已经编写了一些东西让你的div的外观和行为完全像下拉菜单(好吧,我会咬你的,你为什么这样做?)。

这是使用jQuery的JavaScript代码(我们只在StackOverflow上使用jQuery。开玩笑;那不是真的。好吧,也许这有点真实)

 < script type =text / javascript> 
$(function(){
$('#dropdown-selection')。val($('#dropdown span:visible')。text());
});
< / script>

现在,只要您确保将隐藏字段包含在回传的表单中到您的目标PHP页面,您将获得span标签的价值。



我已经对您如何去了解了很多假设在这里设置你的页面。纠正任何与现实无关的部分。


Ok, i have creted my own drop down box. I have a Div, and in the div i have a span. I need to grab that inner span text. How do i do that? The span's text does change. I am 15, so give me a break! I need some help! This is were the values are picket.

    <div>
 <select id="test_select" style="display:none;">
        <option value="1">Graphic Designer</option>
        <option value="2">Animator</option>
        <option value="7">Announcer</option>
        <option value="11">Web Developer</option>
        <option value="12">Judge</option>
        </select>

</div>

That is what needs to be gotten:

<div class="custom-select">
    <span>Select Option</span>
    <ul>        
    </ul>
</div>

解决方案

Ok, you're going to need more than just PHP to get this done; you'll need JavaScript as well.

Let's start with your HTML. I'm assuming your rendered output looks like the following and I won't question why you're doing it this way.

<div id="dropdown">
    <span>Option One</span>
    <span>Option Two</span>
    <span>Option Three</span>
</div>

So there's my guess at your HTML.

To post a value back to PHP, you're also going to need a way to capture the selected value in an input field that can be posted with a form. A hidden input will probably be the best option for you.

<input type="hidden" name="dropdown-selection" id="dropdown-selection" />

So that's our markup done. Next you'll need to grab the selected option from your div and stick it into the hidden field. I'm assuming you've coded something to render your div to look and behave exactly like a dropdown (ok, I'll bite. Why ARE you doing it this way?).

This is the JavaScript code using jQuery (we only use jQuery on StackOverflow. Just kidding; that's not true. Well, maybe it's a little bit true)

<script type="text/javascript">
    $(function () {
        $('#dropdown-selection').val($('#dropdown span:visible').text());
    });
</script>

Now, as long as you've ensured that the hidden field is contained within a form that posts back to your target PHP page, you'll have the value of the span tag available to you.

I've made quite a few assumptions about how you've gone about setting up your page here. Correct me on any parts that don't tie into reality.

这篇关于如何在PHP中获取span的内部文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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