Javascript 获取onclick的数据
HTML:Javascript 获取onclick的数据,javascript,html,Javascript,Html,HTML:这是一个文本 Javascript: <script> function myFunc(){ //code something to console.log the HTML content of the tag //in this case it will console.log "This is a text" } </script> 函数myFunc(){ //为console.log编写代码标记的HTML内容 //在这种情况下,它将显示co
这是一个文本
Javascript:
<script>
function myFunc(){
//code something to console.log the HTML content of the tag
//in this case it will console.log "This is a text"
}
</script>
函数myFunc(){
//为console.log编写代码标记的HTML内容
//在这种情况下,它将显示console.log“这是一个文本”
}
我认为这在jquery中很容易实现,但它最近有点过时,所以我想用纯Javascript的方式来实现。我很想听听你们的回答 这就是你想要的
<p onclick="myFunc(this)">This is a text</p>
function myFunc(e){
console.log(e.innerHTML);
}
jQuery是纯JavaScript。它只是由其他人编写的JavaScriptonclick
属性通常是最好避免的,我们至少已经有20年了。onclick
不是这样做的现代方式。jQuery本身并没有过时,有些人只是不希望有额外的开销this
对myFunc
函数有什么作用?“this”将把当前元素传递给myFunc。这里,当前元素是“p”标记。
function myFunc(){
console.log(window.event.target.innerHTML);
}