单击时获取链接的内部html [英] getting inner html of a link when clicked

查看:97
本文介绍了单击时获取链接的内部html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML链接和一个p标签,如下所示:

I have an HTML Link and a p tag as follows:

<a href="#" onclick="myfun()">Computer Science</a>
<p id="putpara"></p>

这是我的职责:

function myfun() {
            document.getElementById("putpara").innerHTML = this.innerHTML;

        }

但是,当我单击链接时,paragraph tag内的内容将更改为未定义.

However when i click the link the content inside the paragraph tag changes to undefined.

似乎是我犯的一个愚蠢的错误.

Seems like a silly mistake i am making.....Newbie to javascript....

推荐答案

一种解决方案是像这样在函数中发送this参数:

One solution is to send this parameter in your function like this:

html

<a href="#" onclick="myfun(this)">Computer Science</a>
<p id="putpara"></p>

js

window.myfun = function(obj) {
            document.getElementById("putpara").innerHTML = obj.innerHTML;
}

this是指DOM元素.

小提琴

这篇关于单击时获取链接的内部html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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