找到包含x的div类字符串,然后在父div类中更改另一个div类的url

时间:2015-04-28 19:56:24

标签: jquery html

我是html,java的新手。我试图根据div类中的字符串更改URL(图像)。 这是被检查元素:

<div class="container">

    ::before
    <div class="row">
        ::before
        <div id="header"></div>
        <div class="content">
            <div class="adsense"></div>
            <div class="maincontent">
                <script type="text/javascript"></script>
                <div id="schedules" class="col-md-10 col-xs-10 col-sm-10">
                    <div class="header schedule clearfix">
                        ::before
                        <h1></h1>
                        <div class="sub-menu left "></div>

    <div class="subheader schedule clearfix"></div>
    ::after

</div>
<div id="scores">

    <div class="scheduleweek clearfix">
        ::before
        <div class="col-md-5 col-xs-5 col-sm-5 home-team"></div>
        <div class="col-md-2 col-xs-2 col-sm-2 at"></div>
        <div class="col-md-5 col-xs-5 col-sm-5 away-team text-right"></div>
        ::after
    </div>
    <div class="scheduleweek clearfix"></div>
    <div class="scheduleweek clearfix"></div>
    <div class="scheduleweek clearfix"></div>
    <div class="scheduleweek clearfix"></div>
    <div class="scheduleweek clearfix">
        ::before
        <div class="col-md-5 col-xs-5 col-sm-5 home-team">
            <div class="hometeamlogo" style="background-image: url("/img/m25/left/339.png");"></div>
            <div class="name">

                Barons 

            </div>
            <div class="record"></div>

                    WK 2  
                </div>

我正在尝试此代码来更改背景图片网址,基于是否找到“男爵”。

$('.maincontent').has('name:contains("Barons")').find('.hometeamlogo').attr('sty‌​le', 'background-image: url("http://i.imgur.com/0YEAZo6.png");');

它不起作用。现在,如果只是编写代码来替换这个特定的URL(/img/m25/left/339.png)更容易,我也都是这样。但我真的很想让图像得到改变。

1 个答案:

答案 0 :(得分:1)

您可以使用

找到包含特定文本的元素

$( "div:contains('Text To Look')")

检查出来:

var teamLogoElement = $( "div:contains('Barons')").parent(".home-team").find('.hometeamlogo');

teamLogoElement.attr("style","background-image: url(http://dummyimage.com/100x100/000/fff)");
.hometeamlogo {
  width: 100px;
  height:100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">


    <div class="row">

        <div id="header"></div>
        <div class="content">
            <div class="adsense"></div>
            <div class="maincontent">
                <script type="text/javascript"></script>
                <div id="schedules" class="col-md-10 col-xs-10 col-sm-10">
                    <div class="header schedule clearfix">
                  
                        <h1></h1>
                        <div class="sub-menu left "></div>

    <div class="subheader schedule clearfix"></div>


</div>
<div id="scores">

    <div class="scheduleweek clearfix">
    
        <div class="col-md-5 col-xs-5 col-sm-5 home-team"></div>
        <div class="col-md-2 col-xs-2 col-sm-2 at"></div>
        <div class="col-md-5 col-xs-5 col-sm-5 away-team text-right"></div>

    </div>
    <div class="scheduleweek clearfix"></div>
    <div class="scheduleweek clearfix"></div>
    <div class="scheduleweek clearfix"></div>
    <div class="scheduleweek clearfix"></div>
    <div class="scheduleweek clearfix">
   
        <div class="col-md-5 col-xs-5 col-sm-5 home-team">
            <div class="hometeamlogo" style="background-image: url("/img/m25/left/339.png");"></div>
            <div class="name">

                Barons 

            </div>
            <div class="record"></div>

                    WK 2  
                </div>