简体   繁体   English

在html span元素之后添加文本

[英]adding text after an html span element

I have an animation that I need to edit, and it is causing me some problems. 我有一个动画需要编辑,这给我带来了一些问题。

On the page I have a chevron image, and then a text container with text more or less . 在网页上我有一个字形图像,然后用文本的文本容器moreless This info is describing a person and if you click on either the chevron or the text, the window expands so that the user can read all of the information. 此信息描述的是人,如果您单击V形或文字,则窗口将展开,以便用户可以阅读所有信息。

If I click on more the animation expands, (which is good), and the text switches to less . 如果单击more ,动画将扩展(很好),并且文本切换为less However the problem is that if I were to click on the chevron then the animation expands, but the text doesn't change, so I wind up with an already expanded animation, and a text stating more when it should state less . 但问题是,如果我点击字形那么动画扩大,但文字不会改变,所以我风与一个已经展开动画,文本,说明more时候应该注明less

I don't have a fiddle or codepen, but I do have some images that describe whats going on. 我没有小提琴或Codepen,但是我有一些描述发生了什么的图像。

This is before the animation 这是动画之前

在此处输入图片说明

This is after if I click on the chevron, and not the actual text, the text doesn't change. 这是在单击人字形而不是实际文本之后的情况,该文本不会更改。

在此处输入图片说明

My JS is a little long. 我的JS有点长。 (its also written in coffee script) (它也用咖啡脚本编写)

  personMore: ->
    $('a.moreLink').each ->
      $thisMore = $(this).children('span.moreText')
      $thisLess = $(this).children('span.lessText')
      $(this).on 'click', (e) ->
        e.preventDefault()
        return
      $thisMore.on 'click', (e) ->
        $allPeople = $('.person')
        $thisPerson = $(this).parents('.isotope')
        $moreLink = $(this)
        $lessLink = $(this).siblings('.lessText')
        $thisP = $(this).parents('.moreLink').siblings('p')
        $moreLink.hide()
        $lessLink.show()
        $thisP.toggleClass 'moreActive'
        $thisPerson.removeClass('someonesActive').addClass 'activePerson'
        e.preventDefault()
        return
      $thisLess.on 'click', (e) ->
        $allPeople = $('.person')
        $thisPerson = $(this).parents('.isotope')
        $lessLink = $(this)
        $moreLink = $(this).siblings('.moreText')
        $thisP = $(this).parents('.moreLink').siblings('p')
        $openItems = $('.moreActive').size()
        $lessLink.hide()
        $moreLink.show()
        $thisP.toggleClass 'moreActive'
        e.preventDefault()
        if $openItems == 1
          $('.person').removeClass 'activePerson'
        return
      return
    return

My view is 我的看法是

.row
  .person.col-sm-4.isotope
    %a{href: "https://www.linkedin.com/in/harambe", target: "window"}
      %h3 Harambe
    %h4 Worlds Greatest Gorilla
    = image_tag "harambe.jpg"
    %p
      Hot chicken gochujang kombucha artisan. Cornhole snackwave enamel pin helvetica, listicle pabst fingerstache blog. Neutra butcher tote bag activated charcoal, semiotics organic pitchfork forage woke. Brooklyn activated charcoal put a bird on it jean shorts. Fam subway tile readymade skateboard heirloom, ugh shabby chic vinyl echo park bespoke whatever. Disrupt microdosing vice tilde, tattooed chia mlkshk humblebrag master cleanse swag kickstarter. Activated charcoal gastropub vinyl, banjo raclette 90's deep v celiac bitters meh.
    %a.sliding.moreLink{:href => "#"}
      %span.moreText more
      %span.lessText less
    %a.moreLink{:href => "#"}
      %span.moreText= image_tag "moreArrow.png", alt: "more / less"
      %span.lessText= image_tag "moreArrow.png", alt: "more / less"

Finally my CSS 最后是我的CSS

.aboutPeople .person a.moreLink{
  text-align: center;
  display: block;
  font-style: italic;
  position: relative;
  z-index: 99;
  background: #ebebeb;
  padding-bottom: 20px;
  width: 80%;
  margin: 0 auto;
}

It makes sense cause a.more_link with with images has no text inside. 这是有道理的,因为带有图像的a.more_link内部没有文本。

Anyway, I think the view is too complex for what you are trying to achieve and it makes the js way more complex and dependant. 无论如何,我认为对于您要实现的目标而言,视图太复杂了,这使js变得更加复杂和依赖。

Reducing the links to just one and changing one class, you could get the same result and everything gets more neat. 减少到一个的链接并更改一个类,您可以得到相同的结果,并且所有内容都变得更加简洁。 I will try to reduce it more to just have one class on .person and the styling inside dependant on it. 我将尝试更多地减少它,以使.person上只有一个类,并且依赖它的内部样式。 Maybe even you can avoid using someonesActive with CSS3 ~ selector relative to .activePerson ... 也许连你能避免使用someonesActive与CSS3 ~选择相对.activePerson ...

.row
  .person.col-sm-4.isotope
    %a{href: "https://www.linkedin.com/in/harambe", target: "window"}
      %h3 Harambe
    %h4 Worlds Greatest Gorilla
    = image_tag "harambe.jpg"
    %p
      Hot chicken gochujang kombucha artisan. Cornhole snackwave enamel pin helvetica, listicle pabst fingerstache blog. Neutra butcher tote bag activated charcoal, semiotics organic pitchfork forage woke. Brooklyn activated charcoal put a bird on it jean shorts. Fam subway tile readymade skateboard heirloom, ugh shabby chic vinyl echo park bespoke whatever. Disrupt microdosing vice tilde, tattooed chia mlkshk humblebrag master cleanse swag kickstarter. Activated charcoal gastropub vinyl, banjo raclette 90's deep v celiac bitters meh.
    %a.sliding.moreLink{:href => "#"}
      %span.moreText more
      %span.lessText less
      %span.lessText= image_tag "moreArrow.png", alt: "more / less"

less: 减:

.aboutPeople .person a.moreLink{
  text-align: center;
  display: block;
  font-style: italic;
  position: relative;
  z-index: 99;
  background: #ebebeb;
  padding-bottom: 20px;
  width: 80%;
  margin: 0 auto;
  .lessText{
    display: none;
  }
  &.open{
    .moreText{
      display: none;
    }
    .lessText{
      display: block;
    }
  }
}

coffe (sorry if the syntax is not right) 咖啡(对不起,如果语法不正确)

  personMore: ->
    $('a.moreLink').on 'click', (e) ->
      e.preventDefault()
      $this = $(this)
      $allPeople = $('.person')
      $this.toggleClass 'open'
      $this.siblings('p').toggleClass 'moreActive'
      if $this.hasClass('open')
        $allPeople.addClass('someonesActive').removeClass 'activePerson'
        $this.cosest('.person').removeClass('someonesActive').addClass 'activePerson'
      else
        $this.cosest('.person').removeClass 'activePerson'
      if $('.person.activePerson').length <= 0
        $allPeople.removeClass('someonesActive')
      return
    return

I tried to imagine what you were trying to do, but this is just a thought. 我试图想象你想做什么,但这只是一个想法。

The issue is with how you are defining the $moreLink and $lessLink variables: 问题在于您如何定义$ moreLink和$ lessLink变量:

$moreLink = $(this)
$lessLink = $(this).siblings('.lessText')

In this snippet, you are only getting the clicked element as $moreLink and you are only getting the span with class lessText adjacent to the clicked element as $lessLink . 在此代码段中,您只获得了clicked元素为$moreLink ,并且只获得了与clicked元素相邻的span类为lessText的跨度为$lessLink

You actually want to hide and show both sets of spans when an element is clicked. 您实际上想在单击元素时隐藏并显示两组跨度。

If there is only a single one of these animations on a page, you can switch your selectors to: 如果页面上只有这些动画之一,则可以将选择器切换为:

$moreLink = $('span.moreText')
$lessLink = $('span.lessText')

If there are multiple on a page, you'll need to either add an extra identifier to each animation or filter the elements another way, ie: 如果页面上有多个,则需要为每个动画添加一个额外的标识符,或者以其他方式过滤元素,即:

$moreLink = $(this).closest('.isotope').find('span.moreText')
$lessLink = $(this).closest('.isotope').find('span.lessText')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM