gpt4 book ai didi

retrieve id according to the class(根据类检索id)

转载 作者:bug小助手 更新时间:2023-10-26 21:29:13 25 4
gpt4 key购买 nike



I am working with a chatbot interface and I need to retrieve the message id and add it to the submit function. I attempted to use $(".msf-text").id, but it returned undefined. How can I retrieve the id? It is important to note that there are multiple messages within the msg-text class, and I would like the submit function to contain the id of the submitted message.

我正在使用一个聊天机器人接口,我需要检索消息ID并将其添加到提交函数中。我尝试使用$(“.msf-Text”).id,但返回未定义。我怎样才能取回身份证?需要注意的是,msg-Text类中有多个消息,我希望Submit函数包含已提交消息的ID。


randomeIndex = getRandomInt(100000000)
message_index = randomeIndex
<div class="msg-text" id="message-${message_index}">${text}</div>


    msgerForm.addEventListener("submit", event => {


const message_id = $(".msf-text").id
console.log("message id is " + message_id)
}


更多回答

$(".msf-text") returns a jquery object containing an array of all elements with the msf-text class. What you've done is basically like doing [{id:0},{id:1}].id. You have to select which element in that array you're trying to get the id of.

$(“.msf-Text”)返回一个jQuery对象,其中包含带有msf-Text类的所有元素的数组。您所做的基本上与执行[{id:0},{id:1}].id类似。您必须选择要获取id的数组中的哪个元素。

@JosephWebber I tried $(".msf-text")[0] and it's still undefined

@JosephWebber我尝试了$(“.msf-Text”)[0],但仍未定义

I didn't realize you had a typo in your question (msf-text vs msg-text). Change it to msg-text and it should work.

我没有意识到您的问题中有一个拼写错误(msf-text与msg-text)。将其更改为msg-text,它应该可以工作。

优秀答案推荐

Use jQuery::prop() method to access a DOM property of the first element in a jQuery object:

使用jQuery::Prop()方法访问jQuery对象中第一个元素的DOM属性:


https://api.jquery.com/prop/

Https://api.jquery.com/prop/


(you made a type also .msf-text => .msg-text)

(您还输入了.msf-Text=>.msg-Text)


const message_id = $(".msg-text").prop('id');

Or (make sure the element exists):

或(确保元素存在):


const message_id = $(".msg-text")[0].id;


You may consider not using jQuery. Here's a minimal reproducable example
to retrieve the last element of a collection of specific elements.

您可以考虑不使用jQuery。下面是一个最小的可重现示例,用于检索特定元素集合的最后一个元素。




// assign id to all .msg-text for demo
document.querySelectorAll(`.msg-text`)
.forEach((msg, i) => msg.id = `message-${i + 1}`);

// retrieve the last message id:
console.log([...document.querySelectorAll(`.msg-text`)].pop().id);

// when the elements are embbeded in a root element
// (for demo this is the case)
// you can use the :last-child pseudo selector
console.log(document.querySelector(`.msg-text:last-child`).id);

<div>
<div class="msg-text">message 1</div>
<div class="msg-text">message 2</div>
<div class="msg-text">message 3</div>
<div class="msg-text">message 4</div>
<div class="msg-text">message 5</div>
<div class="msg-text">message 8</div>
<div class="msg-text">message 7</div>
</div>




更多回答

$(".msf-text")[0] is undefined, it's strange that it returns no element there, hmm

$(“.msf-Text”)[0]未定义,奇怪的是它没有返回任何元素,嗯

@Lucia you made a type, should be .msg-text

@Lucia你打了一个字,应该是.msg-text

oh yes, the typo, $(".msg-text")[0].id return an empty string ' ' . But I can see the message id is in the DOM

哦,是的,拼写错误$(“.msg-text”)[0].id返回空字符串‘’。但是我可以看到消息ID在DOM中

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com