如何获取传递给javascript函数的元素的值? [英] How to get the value of the element passed to javascript function?
本文介绍了如何获取传递给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元素)
useinnerText
Property for getting the text
the keywordthis
refers to thewindow
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屋!
查看全文