查询方法的问题,从html data- *属性翻译一次

时间:2017-12-01 13:24:41

标签: jquery html5

所以我正在尝试实现一种方法来翻译我的页面中的内容而不使用多余的html5 data- *属性来刷新它,在这种情况下,data-en用于存储英语翻译文本。我正在从西班牙语翻译成英语。

我的代码做了什么,或者应该做什么,因为它不起作用:

- 使用数据属性“data-en”存储所有p元素。 - 通过p元素与for。 - 将“data-en”的值存储到变量“trans”中。 - 用“trans”变量替换p元素的文本。

以下是代码:

$(document).ready(function(){
var i = 0;
var trans;
$('.settingswheel').click(function(){
atraducir = $('p[data-en]');
for (i = 0; i < atraducir.length; i++) {
  trans = atraducir[i].text(atraducir[i].data('en'));
  atraducir[i].text(trans);      
}
});
});
.memory{display:none; width:100%; height:100%; position:absolute; top:0px; left:0px;}

.currentmemory{display:block;}

 .postit {
  position:absolute; 
  overflow:hidden;
  left:1070px; 
  top:-155px; 
  padding-left:10px;
  line-height: 1;   
  width: 275px;    
  margin: 0px;    
  min-height:250px;
  max-height:250px;
  padding-top:35px; 
  border:1px solid #E8E8E8;  
  border-top:60px solid #fdfd86;
  font-family:'Reenie Beanie';    
  font-size:22px;      
  border-bottom-right-radius: 60px 5px;
  display:inline-block;    
  background: #ffff88; /* Old browsers */
  background: -moz-linear-gradient(-45deg, #ffff88 81%, #ffff88 82%, #ffff88 82%, #ffffc6 100%); /* FF3.6+ */
  background: -webkit-gradient(linear, left top, right bottom, color-stop(81%,#ffff88), color-stop(82%,#ffff88), color-stop(82%,#ffff88), color-stop(100%,#ffffc6)); /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(-45deg, #ffff88 81%,#ffff88 82%,#ffff88 82%,#ffffc6 100%); /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(-45deg, #ffff88 81%,#ffff88 82%,#ffff88 82%,#ffffc6 100%); /* Opera 11.10+ */
  background: -ms-linear-gradient(-45deg, #ffff88 81%,#ffff88 82%,#ffff88 82%,#ffffc6 100%); /* IE10+ */
  background: linear-gradient(135deg, #ffff88 81%,#ffff88 82%,#ffff88 82%,#ffffc6 100%); /* W3C */
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffff88', endColorstr='#ffffc6',GradientType=1 ); /* IE6-9 fallback on horizontal gradient */
}
 
.postit:after {     
  content: "";
  position:absolute; 
  z-index:-1;
  right:-0px; bottom:20px;
  width:200px;
  height: 25px;
  background: rgba(0, 0, 0, 0.2);
  box-shadow:2px 15px 5px rgba(0, 0, 0, 0.40);
  -moz-transform: matrix(-1, -0.1, 0, 1, 0, 0);
  -webkit-transform: matrix(-1, -0.1, 0, 1, 0, 0);
  -o-transform: matrix(-1, -0.1, 0, 1, 0, 0);
  -ms-transform: matrix(-1, -0.1, 0, 1, 0, 0);
  transform: matrix(-1, -0.1, 0, 1, 0, 0);
}

.settingswheel{
	position:absolute; 
	bottom:22px; 
	right:15px; 
	cursor:pointer;
	z-index:10001;
	-moz-transition: transform 2.5s;
    -webkit-transition: transform 2.5s;
    transition: transform 2.5s;
}

.settingswheel:hover{
  transform: rotate(360deg);
  -moz-transform: rotate(360deg);
  -webkit-transform: rotate(360deg);
}

.settingsin{z-index:2; font-size:60px; font-family: 'Covered By Your Grace', cursive;  display:flex; justify-content:flex-start; align-items:center; height:80px; width:200px;  background-color:rgb(255, 255, 136); position:absolute; bottom:5px; left:5px; transition: all 1.5s ease;}
.settingsout{z-index:2; font-size:60px; font-family: 'Covered By Your Grace', cursive;  display:flex; justify-content:center; align-items:center; padding:15px; background-color:rgb(255, 255, 136); position:absolute; bottom:5px; left:-202px; transition: all 1.5s ease;}

.menu{
	list-style:none; 
	bottom:8px; 
	position:absolute; 
	font-family: 'Covered By Your Grace', cursive; 
	font-weight:300; 
	width:200px;
	z-index:1;
}

.menu ul li{font-size:22px; margin-top:8px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="postit">
  <img class="settingswheel" src="images/settings.png">
  <span class="settingsin" data-en="Settings">Ajustes</span>
  <ul class="menu">
    <li data-en="Full screen">Pantalla completa:  <span id="fullscreentoggler" value="F" onclick="toggleFullScreen()" data-en="yes">si</span></li>
	<li data-en="Language">Idioma:  <span id="languagetoggler" onclick="toggleLanguage()" >español</span></li>
	<li data-en="Slides">Diapositivas:  <span class="resta">< </span><span class="segundos">5s</span><span class="suma"> ></span></li>
  </ul>
</div>

<div class="memories1container" style="width:550px; height:600px; position:relative; top:42px; left:70px; transform: rotate(-1deg); ">
    <div class="memory fade currentmemory" style="">
	  <div class="p1" style="background-image: url('images/IMG-20170902-WA0031.jpg');"><div class="pin1"></div><div class="pin2"></div><div class="pin3"></div><div class="pin4"></div></div>
	  <div class="p2" style="background-image: url('images/IMG-20170610-WA0040.jpg');"><div class="pin1"></div><div class="pin2"></div><div class="pin3"></div><div class="pin4"></div></div>
	  <p data-mem="1" data-en="Walking with my churri through the gigantic streets of Washington, District of Columbia hehehe. Anda mujer! we are a couple of the most beautiful ..." style="">Paseando con mi churri por las calles gigaaaantes de Washington, Distrito de Columbia jejeje. Anda mujer! pero si somos una pareja de lo mas guapetona...</p>
	  <p data-mem="2" data-en="specially TWO !" style="">especialmente TÚ !</p>
	</div>
</div>

2 个答案:

答案 0 :(得分:0)

您可以使用.text(fn)

$('.settingswheel').click(function () {
    $('p[data-en]').text(function () {
        return $(this).data('en');
    });
});

在代码段中,我使用了<BUTTON>来附加click事件处理程序。

&#13;
&#13;
$(document).ready(function() {
  $('.settingswheel').click(function() {
    $('p[data-en]').text(function() {
      return $(this).data('en');
    });
  });
});
&#13;
.memory {
  display: none;
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0px;
  left: 0px;
}

.currentmemory {
  display: block;
}

.postit {
  position: absolute;
  overflow: hidden;
  left: 1070px;
  top: -155px;
  padding-left: 10px;
  line-height: 1;
  width: 275px;
  margin: 0px;
  min-height: 250px;
  max-height: 250px;
  padding-top: 35px;
  border: 1px solid #E8E8E8;
  border-top: 60px solid #fdfd86;
  font-family: 'Reenie Beanie';
  font-size: 22px;
  border-bottom-right-radius: 60px 5px;
  display: inline-block;
  background: #ffff88;
  /* Old browsers */
  background: -moz-linear-gradient(-45deg, #ffff88 81%, #ffff88 82%, #ffff88 82%, #ffffc6 100%);
  /* FF3.6+ */
  background: -webkit-gradient(linear, left top, right bottom, color-stop(81%, #ffff88), color-stop(82%, #ffff88), color-stop(82%, #ffff88), color-stop(100%, #ffffc6));
  /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(-45deg, #ffff88 81%, #ffff88 82%, #ffff88 82%, #ffffc6 100%);
  /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(-45deg, #ffff88 81%, #ffff88 82%, #ffff88 82%, #ffffc6 100%);
  /* Opera 11.10+ */
  background: -ms-linear-gradient(-45deg, #ffff88 81%, #ffff88 82%, #ffff88 82%, #ffffc6 100%);
  /* IE10+ */
  background: linear-gradient(135deg, #ffff88 81%, #ffff88 82%, #ffff88 82%, #ffffc6 100%);
  /* W3C */
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffff88', endColorstr='#ffffc6', GradientType=1);
  /* IE6-9 fallback on horizontal gradient */
}

.postit:after {
  content: "";
  position: absolute;
  z-index: -1;
  right: -0px;
  bottom: 20px;
  width: 200px;
  height: 25px;
  background: rgba(0, 0, 0, 0.2);
  box-shadow: 2px 15px 5px rgba(0, 0, 0, 0.40);
  -moz-transform: matrix(-1, -0.1, 0, 1, 0, 0);
  -webkit-transform: matrix(-1, -0.1, 0, 1, 0, 0);
  -o-transform: matrix(-1, -0.1, 0, 1, 0, 0);
  -ms-transform: matrix(-1, -0.1, 0, 1, 0, 0);
  transform: matrix(-1, -0.1, 0, 1, 0, 0);
}

.settingswheel {
  position: absolute;
  bottom: 22px;
  right: 15px;
  cursor: pointer;
  z-index: 10001;
  -moz-transition: transform 2.5s;
  -webkit-transition: transform 2.5s;
  transition: transform 2.5s;
}

.settingswheel:hover {
  transform: rotate(360deg);
  -moz-transform: rotate(360deg);
  -webkit-transform: rotate(360deg);
}

.settingsin {
  z-index: 2;
  font-size: 60px;
  font-family: 'Covered By Your Grace', cursive;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  height: 80px;
  width: 200px;
  background-color: rgb(255, 255, 136);
  position: absolute;
  bottom: 5px;
  left: 5px;
  transition: all 1.5s ease;
}

.settingsout {
  z-index: 2;
  font-size: 60px;
  font-family: 'Covered By Your Grace', cursive;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 15px;
  background-color: rgb(255, 255, 136);
  position: absolute;
  bottom: 5px;
  left: -202px;
  transition: all 1.5s ease;
}

.menu {
  list-style: none;
  bottom: 8px;
  position: absolute;
  font-family: 'Covered By Your Grace', cursive;
  font-weight: 300;
  width: 200px;
  z-index: 1;
}

.menu ul li {
  font-size: 22px;
  margin-top: 8px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="settingswheel">settingswheel</button>
<div class="postit">

  <span class="settingsin" data-en="Settings">Ajustes</span>
  <ul class="menu">
    <li data-en="Full screen">Pantalla completa: <span id="fullscreentoggler" value="F" onclick="toggleFullScreen()" data-en="yes">si</span></li>
    <li data-en="Language">Idioma: <span id="languagetoggler" onclick="toggleLanguage()">español</span></li>
    <li data-en="Slides">Diapositivas: <span class="resta">< </span><span class="segundos">5s</span><span class="suma"> ></span></li>
  </ul>
</div>

<div class="memories1container" style="width:550px; height:600px; position:relative; top:42px; left:70px; transform: rotate(-1deg); ">
  <div class="memory fade currentmemory" style="">
    <div class="p1" style="background-image: url('images/IMG-20170902-WA0031.jpg');">
      <div class="pin1"></div>
      <div class="pin2"></div>
      <div class="pin3"></div>
      <div class="pin4"></div>
    </div>
    <div class="p2" style="background-image: url('images/IMG-20170610-WA0040.jpg');">
      <div class="pin1"></div>
      <div class="pin2"></div>
      <div class="pin3"></div>
      <div class="pin4"></div>
    </div>
    <p data-mem="1" data-en="Walking with my churri through the gigantic streets of Washington, District of Columbia hehehe. Anda mujer! we are a couple of the most beautiful ..." style="">Paseando con mi churri por las calles gigaaaantes de Washington, Distrito de Columbia jejeje. Anda mujer! pero si somos una pareja de lo mas guapetona...</p>
    <p data-mem="2" data-en="specially TWO !" style="">especialmente TÚ !</p>
  </div>
</div>
&#13;
&#13;
&#13;

使用.eq(index)访问索引处的元素,然后.data()可以使用.text()

$('.settingswheel').click(function () {
    atraducir = $('p[data-en]');
    for (i = 0; i < atraducir.length; i++) {
        var elem = atraducir.eq(i); //Get element at index
        trans = elem.data('en'); //Get Data
        elem.text(trans); //Set text
    }
});

&#13;
&#13;
$(document).ready(function() {
  var i = 0;
  var trans;
  $('.settingswheel').click(function() {
    atraducir = $('p[data-en]');
    for (i = 0; i < atraducir.length; i++) {
    var elem = atraducir.eq(i);
      trans = elem.data('en');
      elem.text(trans);
    }
  });
});
&#13;
.memory {
  display: none;
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0px;
  left: 0px;
}

.currentmemory {
  display: block;
}

.postit {
  position: absolute;
  overflow: hidden;
  left: 1070px;
  top: -155px;
  padding-left: 10px;
  line-height: 1;
  width: 275px;
  margin: 0px;
  min-height: 250px;
  max-height: 250px;
  padding-top: 35px;
  border: 1px solid #E8E8E8;
  border-top: 60px solid #fdfd86;
  font-family: 'Reenie Beanie';
  font-size: 22px;
  border-bottom-right-radius: 60px 5px;
  display: inline-block;
  background: #ffff88;
  /* Old browsers */
  background: -moz-linear-gradient(-45deg, #ffff88 81%, #ffff88 82%, #ffff88 82%, #ffffc6 100%);
  /* FF3.6+ */
  background: -webkit-gradient(linear, left top, right bottom, color-stop(81%, #ffff88), color-stop(82%, #ffff88), color-stop(82%, #ffff88), color-stop(100%, #ffffc6));
  /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(-45deg, #ffff88 81%, #ffff88 82%, #ffff88 82%, #ffffc6 100%);
  /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(-45deg, #ffff88 81%, #ffff88 82%, #ffff88 82%, #ffffc6 100%);
  /* Opera 11.10+ */
  background: -ms-linear-gradient(-45deg, #ffff88 81%, #ffff88 82%, #ffff88 82%, #ffffc6 100%);
  /* IE10+ */
  background: linear-gradient(135deg, #ffff88 81%, #ffff88 82%, #ffff88 82%, #ffffc6 100%);
  /* W3C */
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffff88', endColorstr='#ffffc6', GradientType=1);
  /* IE6-9 fallback on horizontal gradient */
}

.postit:after {
  content: "";
  position: absolute;
  z-index: -1;
  right: -0px;
  bottom: 20px;
  width: 200px;
  height: 25px;
  background: rgba(0, 0, 0, 0.2);
  box-shadow: 2px 15px 5px rgba(0, 0, 0, 0.40);
  -moz-transform: matrix(-1, -0.1, 0, 1, 0, 0);
  -webkit-transform: matrix(-1, -0.1, 0, 1, 0, 0);
  -o-transform: matrix(-1, -0.1, 0, 1, 0, 0);
  -ms-transform: matrix(-1, -0.1, 0, 1, 0, 0);
  transform: matrix(-1, -0.1, 0, 1, 0, 0);
}

.settingswheel {
  position: absolute;
  bottom: 22px;
  right: 15px;
  cursor: pointer;
  z-index: 10001;
  -moz-transition: transform 2.5s;
  -webkit-transition: transform 2.5s;
  transition: transform 2.5s;
}

.settingswheel:hover {
  transform: rotate(360deg);
  -moz-transform: rotate(360deg);
  -webkit-transform: rotate(360deg);
}

.settingsin {
  z-index: 2;
  font-size: 60px;
  font-family: 'Covered By Your Grace', cursive;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  height: 80px;
  width: 200px;
  background-color: rgb(255, 255, 136);
  position: absolute;
  bottom: 5px;
  left: 5px;
  transition: all 1.5s ease;
}

.settingsout {
  z-index: 2;
  font-size: 60px;
  font-family: 'Covered By Your Grace', cursive;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 15px;
  background-color: rgb(255, 255, 136);
  position: absolute;
  bottom: 5px;
  left: -202px;
  transition: all 1.5s ease;
}

.menu {
  list-style: none;
  bottom: 8px;
  position: absolute;
  font-family: 'Covered By Your Grace', cursive;
  font-weight: 300;
  width: 200px;
  z-index: 1;
}

.menu ul li {
  font-size: 22px;
  margin-top: 8px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="settingswheel">settingswheel</button>
<div class="postit">

  <span class="settingsin" data-en="Settings">Ajustes</span>
  <ul class="menu">
    <li data-en="Full screen">Pantalla completa: <span id="fullscreentoggler" value="F" onclick="toggleFullScreen()" data-en="yes">si</span></li>
    <li data-en="Language">Idioma: <span id="languagetoggler" onclick="toggleLanguage()">español</span></li>
    <li data-en="Slides">Diapositivas: <span class="resta">< </span><span class="segundos">5s</span><span class="suma"> ></span></li>
  </ul>
</div>

<div class="memories1container" style="width:550px; height:600px; position:relative; top:42px; left:70px; transform: rotate(-1deg); ">
  <div class="memory fade currentmemory" style="">
    <div class="p1" style="background-image: url('images/IMG-20170902-WA0031.jpg');">
      <div class="pin1"></div>
      <div class="pin2"></div>
      <div class="pin3"></div>
      <div class="pin4"></div>
    </div>
    <div class="p2" style="background-image: url('images/IMG-20170610-WA0040.jpg');">
      <div class="pin1"></div>
      <div class="pin2"></div>
      <div class="pin3"></div>
      <div class="pin4"></div>
    </div>
    <p data-mem="1" data-en="Walking with my churri through the gigantic streets of Washington, District of Columbia hehehe. Anda mujer! we are a couple of the most beautiful ..." style="">Paseando con mi churri por las calles gigaaaantes de Washington, Distrito de Columbia jejeje. Anda mujer! pero si somos una pareja de lo mas guapetona...</p>
    <p data-mem="2" data-en="specially TWO !" style="">especialmente TÚ !</p>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

试试这个

$(document).ready(function(){
    $('.settingswheel').click(function(){
        atraducir = $('p[data-en]');

        for (i = 0; i < atraducir.length; i++) {
            atraducir[i].innerText = atraducir[i].dataset.en
        }
    });
});