英文:
how to bold a text in a row of table when it is unread and how to unbold when already read using javascript
问题
我正在尝试将我的行中的文本加粗,并在已单击时取消加粗,就像 Gmail 消息一样。在特定行上,当单击文本时,该行的文本将取消加粗。
<td><a href="/webmcr/birthverifyview/<?= $veri['rreg_transactcode']?>"
class="stretched-link" style="text-decoration: none; color: black;">
<?= $veri['rreg_transactcode'] ?></a></td>
<td><?= $veri['rb_fname'] ?> <?= $veri['rb_mname'] ?> <?= $veri['rb_lname'] ?></td>
<td><?= $veri['rb_createdat'] ?></td>
这是我想要加粗并在打开时取消加粗的行的示例。
英文:
I'm trying to bold text in my row and unbold when its already click just like gmail messages. On a particular row, when the text is clicked the text of that row would get unbold.
<td ><a href="/webmcr/birthverifyview/<?=$veri['rreg_transactcode']?>" class="stretched-link" style="text-decoration: none; color: black;"><?= $veri['rreg_transactcode'] ?></a></td>
<td><?= $veri['rb_fname'] ?> <?= $veri['rb_mname'] ?> <?= $veri['rb_lname'] ?> </td>
<td><?= $veri['rb_createdat'] ?></td>
here is the example of my row that I want to get bold and unbold when its open
答案1
得分: 1
绑定点击事件并在事件处理函数中使用CSS取消粗体文本。以下是示例代码:
const messageIds = JSON.parse(localStorage.getItem("messageIds"));
var messageIdsArr = messageIds ? Array.from(messageIds) : [];
document.querySelectorAll("td").forEach((td) => {
// 检查消息是否已读
if (messageIdsArr.length && messageIdsArr.includes(td.getAttribute("id"))) {
td.style.fontWeight = "normal";
} else {
td.addEventListener("click", removeBold);
}
});
function removeBold(e) {
const target = e.currentTarget;
target.style.fontWeight = "normal";
// 更新本地存储
messageIdsArr.push(target.getAttribute("id"));
localStorage.setItem("messageIds", JSON.stringify(messageIdsArr));
}
td {
border: 1px solid black;
font-weight: bold;
cursor: pointer;
}
<table>
<thead></thead>
<tbody>
<tr>
<td id="1">Message 1</td>
<td id="2">Message 2</td>
<td id="3">Message 3</td>
</tr>
</tbody>
</table>
每条消息都有唯一的ID,然后使用localStorage存储已读消息的ID。因此,即使在页面加载时,已读消息也不会以粗体显示。
请注意,该代码不会在stackoverflow片段中运行,因为片段中限制了localStorage的使用。但在片段之外,它将正常工作。希望这对您有所帮助。
英文:
Bind click Event and on the Event Handler function unbold the text using CSS. Here is the example code:-
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const messageIds = JSON.parse(localStorage.getItem("messageIds"));
var messageIdsArr = messageIds ? Array.from(messageIds) : [];
document.querySelectorAll("td").forEach((td) => {
// Check if message already read or not
if (messageIdsArr.length && messageIdsArr.includes(td.getAttribute("id"))) {
td.style.fontWeight = "normal";
} else {
td.addEventListener("click", removeBold);
}
});
function removeBold(e) {
const target = e.currentTarget;
target.style.fontWeight = "normal";
// Update the LocalStorage
messageIdsArr.push(target.getAttribute("id"));
localStorage.setItem("messageIds", JSON.stringify(messageIdsArr));
}
<!-- language: lang-css -->
td{
border: 1px solid black;
font-weight: bold;
cursor: pointer;
}
<!-- language: lang-html -->
<table>
<thead></thead>
<tbody>
<tr>
<td id="1">Message 1</td>
<td id="2">Message 2</td>
<td id="3">Message 3</td>
</tr>
</tbody>
</table>
<!-- end snippet -->
Unique Id has given to each message. And then use localStorage to store the id of message that has been read. So, even on page load, the read message will not be bold.
Note that, the code will not run on stackoverflow snippet because localStorage is restricted in snippet. But will work fine, outside of snippet. Hope it will be helpful.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论