how to bold a text in a row of table when it is unread and how to unbold when already read using javascript

huangapple go评论61阅读模式
英文:

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.

&lt;td &gt;&lt;a  href=&quot;/webmcr/birthverifyview/&lt;?=$veri[&#39;rreg_transactcode&#39;]?&gt;&quot; class=&quot;stretched-link&quot; style=&quot;text-decoration: none; color: black;&quot;&gt;&lt;?= $veri[&#39;rreg_transactcode&#39;] ?&gt;&lt;/a&gt;&lt;/td&gt;
                                                            &lt;td&gt;&lt;?= $veri[&#39;rb_fname&#39;] ?&gt; &lt;?= $veri[&#39;rb_mname&#39;] ?&gt; &lt;?= $veri[&#39;rb_lname&#39;] ?&gt; &lt;/td&gt;
                                                            &lt;td&gt;&lt;?= $veri[&#39;rb_createdat&#39;] ?&gt;&lt;/td&gt;

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(&quot;messageIds&quot;));
var messageIdsArr = messageIds ? Array.from(messageIds) : [];

document.querySelectorAll(&quot;td&quot;).forEach((td) =&gt; {
  // Check if message already read or not
  if (messageIdsArr.length &amp;&amp; messageIdsArr.includes(td.getAttribute(&quot;id&quot;))) {
    td.style.fontWeight = &quot;normal&quot;;
  } else {
    td.addEventListener(&quot;click&quot;, removeBold);
  }
});

function removeBold(e) {
  const target = e.currentTarget;
  target.style.fontWeight = &quot;normal&quot;;

  // Update the LocalStorage
  messageIdsArr.push(target.getAttribute(&quot;id&quot;));
  localStorage.setItem(&quot;messageIds&quot;, JSON.stringify(messageIdsArr));
}

<!-- language: lang-css -->

td{
  border: 1px solid black;
  font-weight: bold;
  cursor: pointer;
}

<!-- language: lang-html -->

 &lt;table&gt;
  &lt;thead&gt;&lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
       &lt;td id=&quot;1&quot;&gt;Message 1&lt;/td&gt;
       &lt;td id=&quot;2&quot;&gt;Message 2&lt;/td&gt;
       &lt;td id=&quot;3&quot;&gt;Message 3&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
 &lt;/table&gt;

<!-- 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.

huangapple
  • 本文由 发表于 2023年5月14日 12:14:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定