這是HTML代碼的ID或價值:無法獲取點擊的元素
<div class="ribbon-fav" id="fav_id">
<%- if user_signed_in? %>
<%- unless current_user.favorite_texts.exists?(id: text.id) -%>
<%= link_to image_tag("fav-hrt.png", size: "20x18", alt: "Add Favorite", title: " Add Favorite "), :id => 'fav_id_002' %>
<%- else -%>
<%= link_to image_tag("favd-hrt.png", size: "20x18", alt: "Remove from Favorites", title: " Remove from Favorites "), :id => 'fav_id_001' %>
<%- end -%>
<%- else -%>
<%= link_to image_tag("fav-hrt.png", size: "20x18", alt: "Add Favorite", title: " Add Favorite "), favorite_texts_path(text_id: text.id), method: :post, :'data-turbolinks-action' => 'replace' %>
<%- end -%>
下面是JavaScript
console.log("favorite_add_remove.js loaded");
$(document).ready(function(){
console.log("called function");
$("#fav_id a").on("click", function(e) {
e.preventDefault();
//var value = $(this).val();
console.log(e.target.id);
//console.log(value);
});
//f1();
})
我無法獲得鏈接的ID或價值點擊。我如何獲得點擊元素的ID?請幫忙,謝謝。
你試過'e.target.id'嗎?你在另一個函數裏調用函數,這是不好的。在'f1()'以外的點擊事件' –
添加呈現的html也 –
無需編寫函數'f1()'。把所有的代碼放在那裏。 – Harish