javascript 如何使data-attr读取HTML

dojqjjoe  于 2023-03-28  发布在  Java
关注(0)|答案(2)|浏览(111)

bounty还有7天到期。回答此问题可获得+150声望奖励。nanchalantflow希望引起更多关注此问题。

我有一个数据属性工具提示,它只显示文本。现在我想在一个短语中添加<a href="google.com">Lorem</a>。例如tooltip="Hello, this is <a href="google.com">Lorem</a> welcome"
但是我不能实现HTML,因为它将读取它作为文本。我如何使Tooltips/data-attr读取HTML?
我想到了这样的东西:在工具提示中读取确切的特定文本,并将其替换为<a href="google.com">Lorem</a>- but -,这仍然会将其显示为文本。

/* START TOOLTIP STYLES */
[tooltip] {
  position: relative; /* opinion 1 */
}

/* Applies to all tooltips */
[tooltip]::before,
[tooltip]::after {
  text-transform: none; /* opinion 2 */
  font-size: .9em; /* opinion 3 */
  line-height: 1;
  user-select: none;
  pointer-events: none;
  position: absolute;
  display: none;
  opacity: 0;
}
[tooltip]::before {
  content: '';
  border: 5px solid transparent; /* opinion 4 */
  z-index: 1001; /* absurdity 1 */
}
[tooltip]::after {
  content: attr(tooltip); /* magic! */
  
  /* most of the rest of this is opinion */
  font-family: Helvetica, sans-serif;
  text-align: center;
  
  /* 
    Let the content set the size of the tooltips 
    but this will also keep them from being obnoxious
    */
  min-width: 3em;
  max-width: 21em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 1ch 1.5ch;
  border-radius: .3ch;
  box-shadow: 0 1em 2em -.5em rgba(0, 0, 0, 0.35);
  background: #333;
  color: #fff;
  z-index: 1000; /* absurdity 2 */
}

/* Make the tooltips respond to hover */
[tooltip]:hover::before,
[tooltip]:hover::after {
  display: block;
}

/* don't show empty tooltips */
[tooltip='']::before,
[tooltip='']::after {
  display: none !important;
}

/* FLOW: UP */
[tooltip]:not([flow])::before,
[tooltip][flow^="up"]::before {
  bottom: 100%;
  border-bottom-width: 0;
  border-top-color: #333;
}
[tooltip]:not([flow])::after,
[tooltip][flow^="up"]::after {
  bottom: calc(100% + 5px);
}
[tooltip]:not([flow])::before,
[tooltip]:not([flow])::after,
[tooltip][flow^="up"]::before,
[tooltip][flow^="up"]::after {
  left: 50%;
  transform: translate(-50%, -.5em);
}

/* FLOW: DOWN */
[tooltip][flow^="down"]::before {
  top: 100%;
  border-top-width: 0;
  border-bottom-color: #333;
}
[tooltip][flow^="down"]::after {
  top: calc(100% + 5px);
}
[tooltip][flow^="down"]::before,
[tooltip][flow^="down"]::after {
  left: 50%;
  transform: translate(-50%, .5em);
}

/* FLOW: LEFT */
[tooltip][flow^="left"]::before {
  top: 50%;
  border-right-width: 0;
  border-left-color: #333;
  left: calc(0em - 5px);
  transform: translate(-.5em, -50%);
}
[tooltip][flow^="left"]::after {
  top: 50%;
  right: calc(100% + 5px);
  transform: translate(-.5em, -50%);
}

/* FLOW: RIGHT */
[tooltip][flow^="right"]::before {
  top: 50%;
  border-left-width: 0;
  border-right-color: #333;
  right: calc(0em - 5px);
  transform: translate(.5em, -50%);
}
[tooltip][flow^="right"]::after {
  top: 50%;
  left: calc(100% + 5px);
  transform: translate(.5em, -50%);
}

/* KEYFRAMES */
@keyframes tooltips-vert {
  to {
    opacity: .9;
    transform: translate(-50%, 0);
  }
}

@keyframes tooltips-horz {
  to {
    opacity: .9;
    transform: translate(0, -50%);
  }
}

/* FX All The Things */ 
[tooltip]:not([flow]):hover::before,
[tooltip]:not([flow]):hover::after,
[tooltip][flow^="up"]:hover::before,
[tooltip][flow^="up"]:hover::after,
[tooltip][flow^="down"]:hover::before,
[tooltip][flow^="down"]:hover::after {
  animation: tooltips-vert 300ms ease-out forwards;
}

[tooltip][flow^="left"]:hover::before,
[tooltip][flow^="left"]:hover::after,
[tooltip][flow^="right"]:hover::before,
[tooltip][flow^="right"]:hover::after {
  animation: tooltips-horz 300ms ease-out forwards;
}







/* UNRELATED to tooltips */
body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  font-family: sans-serif;
  background: #ededed;
}
main {
  flex: 1 1 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
aside {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #49b293;
  color: #fff;
  padding: 1em;
}
main div {
  text-align: center;
  color: #353539;
}
main span {
  padding: .5em 1em;
  margin: .5em;
  display: inline-block;
  background: #dedede;
}

aside a {
  color: inherit;
  text-decoration: none;
  font-weight: bold;
  display: inline-block;
  padding: .4em 1em;
}
<main>
  <div>
    <span tooltip="I'm up above it!">Up</span>
  </div>
  <div>
    <span tooltip="Slide to the left" flow="left">Left</span>
    <span tooltip="Slide to the right" flow="right">Right</span>
  </div>
  <div>
    <span tooltip="Get Down." flow="down">Down</span>
  </div>
</main>
oxf4rvwz

oxf4rvwz1#

你不能在工具提示中插入HTML。实现这一点的最好方法是创建你自己的div,它只会在悬停时显示,并充当工具提示。
让你开始:
HTML

<main>
    <div id="up">
        <div><a href="#">Some Link</a></div>
        <span>Up</span>
    </div>
    <div>
        <span tooltip="Slide to the left" flow="left">Left</span>
        <span tooltip="Slide to the right" flow="right">Right</span>
    </div>
    <div>
        <span tooltip="Get Down." flow="down">Down</span>
    </div>
</main>

中央支助系统

#up {
    position: relative;
}

#up div {
    display: none;
    position: absolute;
    top: -20px;
    padding-bottom: 20px;
    white-space: nowrap;
}

#up:hover div {
    display: block;
}

你需要引用css position。请记住,父元素也必须是一个定位元素-〉这就是为什么我在#up元素上有一个相对位置。按照这个模式并调整每个元素的位置,你应该能够让其他链接/工具提示就位。

pgky5nke

pgky5nke2#

#element {
    width: 20px;
    height: 20px;
    background: red;
}

#element a {
    display: none;
    padding-left: 30px;
}

#element:hover a {
    display: block;
}
<div id="element">
    <a target="_blank" href="https://docs.devexpress.com/">Link</a>
</div>

参考:http://jsfiddle.net/be46df21/9/

相关问题