我有以下 JS:
$(document).on("click", '.like', function (e) {
$(this).parent().html("<a href = '#' class = 'unlike'><div class = 'heart'></div></a>");
return false;
});
$(document).on("click", '.unlike', function (e) {
$(this).parent().html("<a href = '#' class = 'like'><div class = 'coal'></div></a>");
return false;
});
我还有以下 html:
<div>
45
<a href = "#" class = 'unlike'>
<div class = "heart"></div>
</a>
</div>
我试图使用 JS 将整数 45 返回到控制台,但无法这样做。 45 在 div 中。我正在尝试使用与现有 html 相关的父/子或下一个单击来返回它。关于如何执行此操作有什么建议吗?
我尝试过这样做
id = $(this).parent().text;
console.log(id);
但是这不起作用。
最佳答案
如果您使用的是 Chrome 开发工具或 Firebug:
console.log(value);
在 Internet Explorer 中,您需要启动开发工具并刷新页面或 console.log
行不通的。
返回 <div>
中的值div 上没有 id 标签:
$(document).on("click", '.like', function (e) {
$(e.target).parent().html("<a href = '#' class = 'unlike'><div class = 'heart'></div></a>");
console.log($(e.target).text());
return false;
});
关于javascript - 获取包含单击链接的 DIV 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20486736/