如何获取传递给javascript函数的元素的值? [英] How to get the value of the element passed to javascript function?

查看:81
本文介绍了如何获取传递给javascript函数的元素的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javascript中获取元素内容的方法是什么?该元素使用 this 作为参数传递给javascript函数。如何获取函数中元素的内容?不使用class / id。



我尝试过:



What is the way to get the content of element in javascript ? The element is passed as argument to javascript function using this. How can I get the content of element in the function ? Without using class/id.

What I have tried:

<script>

     function func(obj) {

         alert(obj.html())   // get obj's content. here hello
     }
</script>

<html>
  <body>
              <p> hello<script>func(this)</script>  </p>
  </body>
<html>

推荐答案

使用 innerText 获取的属性文本

关键字是指窗口对象,

这个对象取决于触发它的上下文。(例如,窗口,按钮,任何html元素)



use innerText Property for getting the text
the keyword this refers to the window object,
this object depends on the context from where it is being triggered.( eg, window, button, any html elemement )

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title> 
    <script>

        function func(obj) {
            
            alert(obj.innerText);
        }
    </script> 

</head>
<body>


    <p onclick="func(this)">
        hello 
    </p>


</body>
</html>


这篇关于如何获取传递给javascript函数的元素的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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