首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何判断<img>是否有href属性?

问如何判断<img>是否有href属性?
EN

Stack Overflow用户
提问于 2019-12-12 04:49:55
回答 1查看 83关注 0票数 0

因此,我有一个使用JavaScript淡入淡出图像的简单幻灯片,其中除一个元素外,所有img元素都没有href属性。我想把图片的href属性放到标题的href属性中(实际上是img alt属性),这样当特定的幻灯片显示时,文本是可点击的,而当幻灯片转到下一个幻灯片时,文本是不可点击的。我在JS方面非常缺乏经验,在网上找不到任何资源来帮助我解决这个特定的问题。下面是我的slideshow.js文件(称为Finley.html)中使用的部分。

代码语言:javascript
复制
<section>
  <!-- Slideshow -->
  <h2 id="caption">Car ride home! First time meeting each other.</h2>
  <img id="slide" src="images/Finley/car_ride_home.jpg" alt="">
  <div id="slides">
    <img src="images/Finley/car_ride_home.jpg" alt="Car ride home! First time meeting each other.">
    <img src="images/Finley/he_lay.jpg" alt="Handsome boy!">
    <img src="images/Finley/awwww.jpg" alt="First photo together">
    <img src="images/Finley/photogenic_af.jpg" alt="Papa, we made it to the community Instagram!" href="https://google.com" target="_blank">
    <img src="images/Finley/first_vet.jpg" alt="First vet trip was successful">
    <img src="images/Finley/blop.jpg" alt="blop.">
    <img src="images/Finley/squishy_cheek.jpg" alt="Car rides make me sleepy">
    <img src="images/Finley/first_beach.jpg" alt="First time at the beach and I got rocked by the waves">
    <img src="images/Finley/sploot.jpg" alt="sploot.">
    <img src="images/Finley/floopy_ears.jpg" alt="My family loves how floopy my ears are!">
    <img src="images/Finley/daisy_gf.jpg" alt="Playing with my friend Daisy!">
  </div>
</section>

slideshow.js:

代码语言:javascript
复制
$(document).ready(function() {
      //declare variables
      var nextSlide = $("#slides img:first-child");
      var nextCaption;
      var nextSlideSource;
      // the function for running the slide show    
      var runSlideShow = function() {
        // write the function 
        $("#caption").fadeOut(1000);
        $("#slide").fadeOut(1000,
          function() { //callback function
            if (nextSlide.next().length == 0) {
              nextSlide = $("#slides img:first-child");
            } else {
              nextSlide = nextSlide.next();
            }
            nextSlideSource = nextSlide.attr("src");
            nextCaption = nextSlide.attr("alt");

            if (nextCaption == "Papa, we made it to the community Instagram!") {
              $("#caption").attr("href", nextSlide.attr("href"); $("#caption").attr("target", nextSlide.attr("target");
                }

                $("#slide").attr("src", nextSlideSource).fadeIn(1000); $("#caption").text(nextCaption).fadeIn(1000);

              }
            ); //callback
          }; //ready

          // start slide show
          var timer1 = setInterval(runSlideShow, 5000);


          // here's one way to code this app without using the toggle event method
          $("#slide").click(function() {
            if (timer1 != null) {
              clearInterval(timer1);
              timer1 = null;
            } else {
              timer1 = setInterval(runSlideShow, 1000);
            }
          });
        })

此代码将标题显示为图像上方的文本,图像将在屏幕上停留5秒钟,然后淡出到下一个图像。为了澄清,我希望标题文本在显示一个图片时显示为Google (实际网站审查)的超链接,然后在下一张图片出现时恢复正常。我试着将21-24行放入来测试标题并设置属性,但这些行阻止了幻灯片的播放(删除这些行以查看它的行为)。也许在第21行的if语句中有一些东西,但我不确定如何继续。如有任何帮助或建议,我们将不胜感激!

EN

回答 1

Stack Overflow用户

发布于 2019-12-12 05:07:00

你可以像这样做:

代码语言:javascript
复制
this.hasAttribute('href');

或

代码语言:javascript
复制
$(this).is('[href]');

More Info

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59293860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档