【问题标题】:Javascript Show/Hide textJavascript 显示/隐藏文本
【发布时间】:2020-12-18 13:44:43
【问题描述】:

我正在尝试创建一个列表,当用户单击该列表时,会显示一个图像及其下方的文本。假设列表 1,我希望它在单击链接时显示“价格:1 美元”。然后对于列表 2,单击链接时图像下方的“价格:2 美元”。理想情况下,与图像显示/隐藏的功能相同。然后当点击离开时,隐藏元素。我对 JS 还很陌生,但到目前为止我收集到的内容如下:

$(function () {
  $(".imgPreview").hide();
  $(".unstyled li a").click(function () {
    $(".imgPreview").show().find("img").attr("src", $(this).attr("href"));

    return false;
  });
  $("body").click(function () {
    $(".imgPreview").hide();

  });
});
* {font-family: 'Segoe UI'; margin: 0; padding: 0; list-style: none;}
body {margin: 10px;}
li {margin: 25px;}
.unstyled, .imgPreview, .showPrice {float: left; width: 50%;}
.unstyled, .imgPreview img {max-width: 100%;}
p {margin: 5px;}

.recycle-button {
    padding: 6px 6px;
    background-color: rgb(53, 189, 208);
    border-radius: 5px;
    color: white;
    margin-bottom: 0px;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div style="margin-bottom: 35px;">
<h3>LIST</h3> </div>
<ul class="unstyled">
  <li><a href="http://placehold.it/350x150" class="recycle-button">List 1</a></li>
  <li><a href="http://placehold.it/350x150" class="recycle-button">List 2</a></li>
  <li><a href="http://placehold.it/350x150" class="recycle-button">List 3</a></li>
  <li><a href="http://placehold.it/350x150" class="recycle-button">List 4</a></li>
  <li><a href="http://placehold.it/350x150" class="recycle-button">List 5</a></li>
  <li><a href="http://placehold.it/350x150" class="recycle-button">List 6</a></li>
</ul>
<div class="imgPreview">
  <img src="" alt="" width="350" height="150"/>
</div>

我的目标是拥有像this 这样的东西。

编辑:

到目前为止,我已经尝试将属性 data-price 添加到元素 以保存价格数据:

<li><a href="http://placehold.it/350x150" class="recycle-button" data-price="Price: $3.20">List 1</a></li>

并让脚本对 imgPreview 执行相同的功能:

$(".imgPreview").show().find("p").attr($(this).attr("data-price"));

我是fiddling,我收到的错误是“脚本错误”。当然,您可以看到我对 JS 的了解有限。

【问题讨论】:

  • 您能否更明确地说明您尝试过的内容、遇到的错误和错误(如果有)。
  • @cape_bsas 绝对。感谢您对澄清的评论。对社区来说相当陌生,对我来说如此赤裸裸。检查帖子上的编辑。

标签: javascript html css menu show-hide


【解决方案1】:

也许你想要这样的东西......

$(function () {
  $(".imgPreview").hide();
  $(".unstyled li a").click(function (event) {
    var listItem = event.target.parentElement;
    var image = $(".imgPreview");
    image.show();
    image.find("img").attr("src", $(this).attr("href"));
    $(image.find("span"))[0].innerHTML="Price = $"+ ($( "li" ).index( listItem ) + 1);
    return false;
  });
  $("body").click(function () {
    $(".imgPreview").hide();

  });
});
* {font-family: 'Segoe UI'; margin: 0; padding: 0; list-style: none;}
body {margin: 10px;}
li {margin: 25px;}
.unstyled, .imgPreview, .showPrice {float: left; width: 50%;}
.unstyled, .imgPreview img {max-width: 100%;}
p {margin: 5px;}

.recycle-button {
    padding: 6px 6px;
    background-color: rgb(53, 189, 208);
    border-radius: 5px;
    color: white;
    margin-bottom: 0px;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div style="margin-bottom: 35px;">
<h3>LIST</h3> </div>
<ul class="unstyled">
  <li><a href="http://placehold.it/350x150" class="recycle-button">List 1</a></li>
  <li><a href="http://placehold.it/350x150" class="recycle-button">List 2</a></li>
  <li><a href="http://placehold.it/350x150" class="recycle-button">List 3</a></li>
  <li><a href="http://placehold.it/350x150" class="recycle-button">List 4</a></li>
  <li><a href="http://placehold.it/350x150" class="recycle-button">List 5</a></li>
  <li><a href="http://placehold.it/350x150" class="recycle-button">List 6</a></li>
</ul>
<div class="imgPreview">
  <img src="" alt="" width="350" height="150"/>
  <span id="priceView"></span>
</div>

【讨论】:

  • 这很有帮助,但价格会逐步上涨。如何为不同的列表项设置自定义价格?例如清单 1:价格 = 13.24 美元,清单 4:价格 = 3.02 美元。
  • @abarkez,为此您必须更改以下行以满足您的要求。 $(image.find("span"))[0].innerHTML="Price = $"+ calculatePrice();此 calculatePrice 将是一个计算价格并返回给您的方法。
猜你喜欢
  • 2015-06-14
  • 1970-01-01
  • 1970-01-01
  • 2013-02-23
  • 1970-01-01
  • 2012-05-17
  • 2017-09-03
  • 2017-02-11
  • 1970-01-01
相关资源
最近更新 更多