简体   繁体   English

如何使用javascript获取特定的子节点类名?

[英]How to Get particular child node class name using javascript?

I want to get particular child node(ie class name).我想获得特定的子节点(即类名)。 how can i do this.我怎样才能做到这一点。 This is my code这是我的代码

    <div id="mess">
                     <h1>Bhashyam School</h1>
                        <p>AC Nagar Main Road, Nellore <br/>Ho, Nellore - 524001  </p>
                     <p class="tel">Telephone: +(91)-9603444376 </p>
                     <p class="email">Email: <a href="mailto:info@citydeal.in">info@citydeal.in</a></p>
                     <p class="web">Website: <a href="http//citydeal.in" target="new">http//citydeal.in</a></p>
                     <p class="description">Hours of Operation : 
                        Monday, Tuesday, Wednesday, Thursday, Friday, Saturday: 09:30 am to 10:00 pm<br>
                        Sunday: Closed , Modes of Payment : 
                        Cash
                     </p>

<form name="contactform" method="post" action="send.php" onsubmit="return getChilds();">
                    <p>
                        <input id="box" type="text" name="telephone" placeholder="Enter Mobile Number"/>

                        <input type="hidden" name="strToSend" id="strToSend"/>
                        <button type="submit" class="button" value="Submit">Get SMS</button>
                    </p>
                </form>
    </div>

    <script type="text/javascript">
        function getChilds()
    {
        var arr=document.getElementById('mess').childNodes, str='';
        for(var i=0; i<arr.length; i++) {
            if(arr[i].innerHTML!=undefined) {
                str+=" "+(arr[i].textContent||arr[i].innerText);
            }
        }
        document.getElementById("strToSend").value=str;
        return true;
    }
    </script>

In the above javascript code i want childNodes with particular class names (ex:p class="tel", p class="email").在上面的 javascript 代码中,我想要具有特定类名的 childNodes(例如:p class="tel",p class="email")。

With jQuery ?用 jQuery 吗?

.children(".yourClass")

Documentation文档

And as I said in comments , without jQuery :正如我在评论中所说,没有 jQuery:

.querySelectorAll(".yourClass")

Documentation文档

And because I'm the nicest person in the world, here is a working JSFiddle with .querySelectorAll() :因为我是世界上最好的人,这里有一个带有.querySelectorAll()的工作 JSFiddle:

http://jsfiddle.net/F8DeQ/ http://jsfiddle.net/F8DeQ/

(In order for you to see the result, I set your hidden input to text ) (为了让您看到结果,我将您的hidden输入设置为text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM