javascript - 如何在 onclick 事件上发送标签的值

标签 javascript jquery html ajax

我正在尝试在单击此 anchor 标记时发送标签的值。任何人都可以建议我传递标签值的最佳方法是什么(标签值动态变化,因此希望在单击 anchor 标记时传递该值到java脚本函数。)

示例代码:

<a class="k-link" onclick="Getvalue("$("#ID").html()">Profile</a>

2

function Getvalue(Id) {
}

最佳答案

使用.text()而不是.html() :

jQuery

$("a").on("click", function(){
   alert($(this).text());
})​

<强> EXAMPLE

因为我们通过 jQuery 绑定(bind)点击事件,所以您不需要内联执行此操作。您的 HTML 将变为:

<a class="k-link">Profile</a>

Unlike the .html() method, .text() can be used in both XML and HTML documents. The result of the .text() method is a string containing the combined text of all matched elements. (Due to variations in the HTML parsers in different browsers, the text returned may vary in newlines and other white space.)

关于javascript - 如何在 onclick 事件上发送标签的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12916492/

相关文章:

javascript - Vue.js,将数据传递给另一条路线上的组件

javascript - 允许 Google Chrome 使用 XMLHttpRequest 从本地文件加载 URL

javascript - 如何只循环播放视频 3 次?

javascript - 从一个 php 文件导航到另一个携带数据数组的文件

javascript - JQuery "contains"不返回任何 html 编码

javascript - OnMouseDown 不适用于 Div

javascript对象,自引用问题

javascript - 如何仅在滚动到元素时将 Animate.css 效果应用于该元素?

php jquery 显示来自 mysql getjson 的数据

javascript - 如何知道用户正在浏览 HTML 的哪一部分