Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript/Jquery获取子元素、减字符串和替换_Javascript_Jquery_Replace_Substr - Fatal编程技术网

Javascript/Jquery获取子元素、减字符串和替换

Javascript/Jquery获取子元素、减字符串和替换,javascript,jquery,replace,substr,Javascript,Jquery,Replace,Substr,我看了很多,但找不到解决问题的方法:我不是JS专家,但这对程序员来说可能很容易 我有一份名单,一份导航名单: <ul id="menu-navbar"> <li id="menu-item-270-en" class="lang-item"> <a href="site.com/en"> English </a> </li> <li id="menu-item-270-pt" c

我看了很多,但找不到解决问题的方法:我不是JS专家,但这对程序员来说可能很容易

我有一份名单,一份导航名单:

<ul id="menu-navbar">
   <li id="menu-item-270-en" class="lang-item">
     <a href="site.com/en">
       English
     </a>
   </li>
   <li id="menu-item-270-pt" class="lang-item">
     <a href="site.com/">
       Português
     </a>
   </li>
</ul>
两件事: 我需要从
中获取文本(英语)以及导航栏右上角的列表

注:英文网站尚未完全运行


非常感谢你

对于您的需求,最好是维护一个映射对象

jQuery
var langListMap=[{
正文:“英文”,
简称:“En”
}, {
案文:“葡萄牙”,
简称:“Pt”
}]
$(文档).ready(函数(){
$。每个($(“a”)函数(i,a){
var sName=getObject($(a).text());
如果(sName){
$(a).text(sName.shortName);
}
});
})
函数getObject(文本){
console.log(文本)
返回langListMap.filter(函数(a){
返回a.text==text.trim();
})[0]
}


要使用纯JavaScript,而不是使用jQuery库,我建议:

'use strict'

// declaring the variable 'abbreviationsMap' as a 'constant'
// to prevent rebinding later in the script (although it can
// still be updated, eg:
// abbreviationsMap.English = 'xy', or
// abbreviationsMap.Welsh = 'cy'
const abbreviationsMap = {
  'English': 'En',
  'Português': 'Pt'
};


// named function to handle the creation of abbreviations,
// with user-supplied options:
function abbreviations(opts) {

  // declaring variables using 'let' to limit the
  // variables to the scope of the block in which
  // they're declared:
  let settings = {

    // the CSS selector by which the elements whose
    // content should be abbreviated should be selected:
    'selector': '.toAbbreviate',

    // the map of full-words-to-abbreviations to use:
    'abbreviations': abbreviationsMap
  };

  // iterating over the properties declared in the
  // opts Object to update the settings:
  for (let property in opts) {

    // if the opts Object has a named property
    // not inherited from its prototype equal to
    // the current property-name:
    if (opts.hasOwnProperty(property)) {

      // we update the same property of the
      // settings Object to match the value
      // from the opts Object:
      settings[ property ] = opts[ property ];
    }
  }

  // finding the elements from the document that match the
  // CSS selector:
  let elements = document.querySelectorAll( settings.selector );

  // converting that NodeList to an Array, using Array.from():
  let elementsArray = Array.from( elements );

  // iterating over the Array:
  elementsArray.forEach(function(abbr) {
    // 'abbr' the first-named argument of the anonymous function
    // is automatically supplied by the function itself, and is
    // a reference to the array-element of the array over which
    // we're iterating.

    // setting the title property of the array-element to
    // offer an explanation of what the abbreviation means,
    // removing any leading/trailing white-space using
    // String.prototype.trim():
    abbr.title = abbr.textContent.trim();

    // updating the textContent of the element to match the
    // abbreviation held in the settings.abbreviation Array,
    // or, if that's a falsy value, simply returns the title
    // of the element:
    abbr.textContent = settings.abbreviations[abbr.title] || abbr.title;

  });

}

// calling the function, and modifying the CSS selector
// to be used within the function:
abbreviations({
  'selector': '.lang-item a'
});
“严格使用”
常量缩写映射={
‘English’:‘En’,
“葡萄牙语”:“葡萄牙语”
};
函数缩写(opts){
让设置={
“选择器”:“.toAbbreviate”,
“缩写”:缩写映射
};
for(让属性在选项中){
如果(选择hasOwnProperty(属性)){
设置[属性]=选择[属性];
}
}
让元素=document.querySelectorAll(settings.selector);
让elementsArray=Array.from(elements);
elementsArray.forEach(函数(缩写){
abbr.title=abbr.textContent.trim();
abbr.textContent=settings.缩写[abbr.title]| | abbr.title;
});
}
缩略语({
“选择器”:“.lang项目a”
});

太棒了!谢谢你的回答!您能告诉我如何将这些值发送回HTML,而不是发送警报吗?它没有显示英语“葡萄牙语”,而是替代了英语“葡萄牙语”。非常感谢你!你的意思是,单击时,用
En
设置一些元素吗?我的意思是,用户不必在加载文档时查看它,而是显示我不能手动执行此操作,因为它是由一个插件自动生成的:c这就是为什么它必须使用Javascript完成的原因。我不希望必须单击链接才能显示短名称,我希望它替换HTML上的文本。非常感谢你抽出时间,Rajesh@我已经更新了我的答案。我还添加了一个纯JS版本。希望它能帮上忙汉克斯·拉杰什:)我也会更新这个问题以备将来搜索。干杯