我一直很难让孩子们进入i标记,我尝试了很多方法并阅读了关于StackOverflow的许多问题,但仍然没有帮助。
这是我的代码:
function markAsRead() { //marked as read function
console.log($(this).children(".gsdfs").text());
}
这是html:
<i class='fas fa-times' style='color: #FFCC00; cursor: pointer;margin-left: 87%; margin-top: -53px; position: absolute; ' onclick='markAsRead()'>
<input type='hidden' value='123456787' class='notification_trackId'>
<p style='color: transparent;' class='gsdfs'>Hello World</p>
</i>
以下是输出:
答案 0 :(得分:1)
您必须将Public Function splitText() As Variant
Dim testValue As Variant
'Trying to import testString from the Sub to split it
testValue = Split(testString, ",")
'Define result of the Public Function
splitText = testValue
End Function
传递给函数,以便可以在函数内部引用它。
请注意::我更改了样式this
和margin-top: -53px
,以便在代码段中可以查看该元素。
color: transparent
function markAsRead(el) { //marked as read function
console.log($(el).children(".gsdfs").text());
}
如果类<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<i class='fas fa-times' style='color: #FFCC00; cursor: pointer;margin-left: 87%; margin-top: 0px; position: absolute; ' onclick='markAsRead(this)'>
<input type='hidden' value='123456787' class='notification_trackId'>
<p style='color: black;' class='gsdfs'>Hello World</p>
</i>
的元素嵌套在其他元素中,则必须使用gsdfs
:
find()
function markAsRead(el) { //marked as read function
console.log($(el).find(".gsdfs").text());
}
答案 1 :(得分:0)
要遍历孩子,可以使用each
:
const parentSelector = ...;
$(parentSelector).children(".gsdfs").each(function () {
console.log($(this).text()); // $(this) is every child.
});
但是您不需要它。.
您要获取gsdfs
的文本。
该元素是类别为p
的{{1}}元素。
所以您需要这样做:
gsdfs
OR
console.log(document.getElementsByClassName('gsdfs')[0].textContent);
答案 2 :(得分:0)
$(this).find(".gsdfs")
将在具有类名称gsdfs的当前元素内返回一个子元素数组。如果您想使用以下代码进行迭代:
$.each($(this).find(".gsdfs"),function(index,element){
console.log(element);
});
为了在当前元素中获取html元素,可以使用JQuery的.html()
函数,如下所示:
console.log($(this).html());
答案 3 :(得分:0)
您也可以尝试此JSFiddle
HTML代码-
<i class='fas fa-times' style='color: #FFCC00; cursor: pointer;margin-left: 87%; margin-top: -53px; position: absolute;'><input type='hidden' value='123456787' class='notification_trackId'><p style='color: transparent;' class='gsdfs'>Hello World</p></i>
JS代码-
$(document).on('click', 'i.fas', function(){
var text = $(this).find(".gsdfs").text();
console.log(text);
alert(text);
});