GVKun编程网logo

Javascript最有效的方法来从ID链接的2个选项卡构建对象(js获取id对应的组件)

12

想了解Javascript最有效的方法来从ID链接的2个选项卡构建对象的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于js获取id对应的组件的相关问题,此外,我们还将为您介绍关于4种Java

想了解Javascript最有效的方法来从ID链接的2个选项卡构建对象的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于js获取id对应的组件的相关问题,此外,我们还将为您介绍关于4种JavaScript实现简单tab选项卡切换的方法_javascript技巧、ECMAScript委员会:JavaScript最基础的14种操作对象的方法、JavaScript – 从Bootstrap的导航标签跳到另一个选项卡的特定选项卡、javascript – 从iphone中的多选框中取消选择最后一个选项的新知识。

本文目录一览:

Javascript最有效的方法来从ID链接的2个选项卡构建对象(js获取id对应的组件)

Javascript最有效的方法来从ID链接的2个选项卡构建对象(js获取id对应的组件)

尝试一下。

var m = {};
var arr1 = [{id : 1,name: 'colors',type: 'list',label:'xxx'},{id : 2,name: 'yyy',type: 'date',label:'yyy'},{id : 3,name: 'dogs',label:'zzz'}];
var arr2 = [{id:1,name:'blue',order:1},{id:1,name:'red',order:2},name:'green',order:3},{id:3,name:'husky',name:'bulldog',name:'dalmatian',order:3}];
for(var i=0;i<arr1.length;i++){
    arr1[i]["options"] = [];
    m[arr1[i]["id"]] = arr1[i];
}
for(var i=0;i<arr2.length;i++){
    if(m.hasOwnProperty(arr2[i]["id"])){
        m[arr2[i]["id"]]["options"].push(arr2[i]);
    }
}
console.log(arr1);
,
let person = [
    {id : 1,label:'zzz'}
];

let hobby = [
    {id:1,order:3}
];

let arr = person.map((item,i) => Object.assign({},item,hobby[i]));

console.log(arr);

4种JavaScript实现简单tab选项卡切换的方法_javascript技巧

4种JavaScript实现简单tab选项卡切换的方法_javascript技巧

本文实例讲解了4种JavaScript实现简单tab选项卡切换的方法,分享给大家供大家参考,具体内容如下
效果图:

 

方法一:for循环+if判断当前点击与自定义数组是否匹配

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>tab切换</title>
  <style type="text/css">
    button {
      width:120px;
      height: 32px;
      line-height: 32px;
      background-color: #ccc;
      font-size: 24px;
    }
    div {
      display: none;
      width:200px;
      height: 200px;
      font-size: 72px;
      color:#ddd;
      background-color: green;
      border:1px solid black;
    }
  </style>
</head>
<body>
  <button>1</button>
  <button>2</button>
  <button>3</button>
  <button>4</button>
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <script type="text/javascript">
  //定义数组并获取数组内各个的节点
  var buttonArr = document.getElementsByTagName("button");
  var divArr = document.getElementsByTagName("div");
  for(var i = 0; i < buttonArr.length;i++) {
    buttonArr[i].onclick = function() {
      //this 
      // alert(this.innerHTML)
      //for循环遍历button数组长度
      for(var j = 0; j < buttonArr.length; j++) {
        //重置所有的button样式
        buttonArr[j].style.backgroundColor = "#ccc";
        //给当前的(点击的那个)那个button添加样式
        this.style.backgroundColor = "yellow";
        //隐藏所有的div
        divArr[j].style.display = "none";
        //判断当前点击是按钮数组中的哪一个?
        if(this == buttonArr[j]) {
          // alert(j);
           //显示点击按钮对应的div
          divArr[j].style.display = "block";
        }
      }
    }
  }
  </script>
</body>
</html>
登录后复制

方法二:自定义index为当前点击

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>tab切换</title>
  <style type="text/css">
    button {
      width:120px;
      height: 32px;
      line-height: 32px;
      background-color: #ccc;
      font-size: 24px;
    }
    div {
      display: none;
      width:200px;
      height: 200px;
      font-size: 72px;
      color:#ddd;
      background-color: green;
      border:1px solid black;
    }
  </style>
</head>
<body>
  <button>1</button>
  <button>2</button>
  <button>3</button>
  <button>4</button>
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <script type="text/javascript">
  var buttonArr = document.getElementsByTagName("button");
  var divArr = document.getElementsByTagName("div");
  for(var i = 0; i < buttonArr.length;i++) {
    buttonArr[i].index = i;
    // buttonArr[i].setAttribute("index",i);
    buttonArr[i].onclick = function() {
      for(var j = 0; j < buttonArr.length; j++) {
        buttonArr[j].style.backgroundColor = "#ccc";
        buttonArr[this.index].style.backgroundColor = "yellow";
        divArr[j].style.display = "none";
        divArr[this.index].style.display = "block";
      }
    }
  }
  
  </script>
</body>
</html>
登录后复制

方法三:className

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>tab</title>
  <style type="text/css">
    * {padding:0; margin:0;}
    button {
      background-color: #ccc;
      width:80px;
      height: 30px;
    }
    .btn-active {
      background-color: yellow;
      font-weight:bold;
      font-size: 14px;
    }
    div{
      width:200px;
      height: 200px;
      font-size: 64px;
      background-color: #0c0;
      display: none;
      color:#fff;
    }
    .div-active {
      display: block;
    }
  </style>
</head>
<body>
  <button>按钮1</button>
  <button>按钮2</button>
  <button>按钮3</button>
  <button>按钮4</button>
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <script type="text/javascript">
  //1.先获取元素
  var buttonList = document.getElementsByTagName("button");
  var divList = document.getElementsByTagName("div");
  //2.添加事件
  for(var i = 0; i < buttonList.length; i++) {
    buttonList[i].index = i;
    buttonList[i].onclick = function() {
      for(var j = 0; j < buttonList.length;j++) {
        buttonList[j].className = "";
        divList[j].className = "";
      }
      this.className = "btn-active";
      divList[this.index].className = "div-active";
    }
  }
  </script>
</body>
</html>
登录后复制

方法四:className+匿名函数的自执行

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>tab</title>
  <style type="text/css">
    * {padding:0; margin:0;}
    button {
      background-color: #ccc;
      width:80px;
      height: 30px;
    }
    .btn-active {
      background-color: yellow;
      font-weight:bold;
      font-size: 14px;
    }
    div{
      width:200px;
      height: 200px;
      font-size: 64px;
      background-color: #0c0;
      display: none;
      color:#fff;
    }
    .div-active {
      display: block;
    }
  </style>
</head>
<body>
  <button>按钮1</button>
  <button>按钮2</button>
  <button>按钮3</button>
  <button>按钮4</button>
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <script type="text/javascript">
  //1.先获取元素
  var buttonList = document.getElementsByTagName("button");
  var divList = document.getElementsByTagName("div");
  //2.添加事件
  for(var i = 0; i < buttonList.length; i++) {
    (function(i){ //匿名函数自执行
       buttonList[i].onclick = function() {
        for(var j = 0; j < buttonList.length;j++) {
          buttonList[j].className = "";
          divList[j].className = "";
        }
        this.className = "btn-active";
        divList[i].className = "div-active";
      }
    })(i)
  }
  </script>
</body>
</html>
登录后复制

希望本文所述对大家学习javascript程序设计有所帮助。

ECMAScript委员会:JavaScript最基础的14种操作对象的方法

ECMAScript委员会:JavaScript最基础的14种操作对象的方法

先贴一个MDN地址,搜的时候搜js实现的方法名

1. 获取对象原型 [ [ GetPrototypeOf ] ]

// js实现: GetPrototypeOf
let obj = {}
let proto = Object.getPrototypeOf(obj)
proto === obj.__proto__ === Object.prototype  // true

2. 设置对象原型 [ [ SetPrototypeOf ] ]

// js实现: SetPrototypeOf
let obj = {}
Object.setPrototypeOf(obj, { a: 1, b: 2 }) // 函数式
obj.__proto__ = { a: 1, b: 2 } // 赋值式
Object.prototype = { a: 1, b: 2 } // 赋值式
// 上面效果一样

3. 获取对象自有属性 [ [ GetOwnProperty ] ]

// js实现: 
// 1. getOwnPropertyDescriptor 获取对象下某一个属性的描述符
// 2. getOwnPropertyDescriptors 获取对象下所有属性的描述符
// 3. getOwnPropertyNames 获取对象下所有属性名(不包括Symbol)
// 4. getOwnPropertySymbols 获取对象下所有Symbol属性名

// 描述符为 configurable enumerable writable value

4. 禁止对象扩展 [ [ PreventExtensions ] ]

// js实现: preventExtensions
let obj = { a: 1 }
Object.preventExtensions(obj 不可新增属性,不可删除,可读)
// obj 变的不可新增属性,可删除属性,可读,可改

5. 获取对象的可扩展性 [ [ IsExtensible ] ]

// js实现: isExtensible
let obj = {}
let extensible = Object.isExtensible(obj) // true

// 使对象变的不可扩展的方法
1. freeze 
2. seal 
3. preventExtensions

6. 拦截对象操作 [ [ DefineOwnProperty ] ]

// js实现: defineProperty 单个属性, defineProperties 多个属性
let obj = { a: 1 }
Object.defineProperty(obj, ''a'', {
    value: 1,
    writable: true,
    configurable: true,
    enumerable: true,
    // 前面是描述符
    // value, writable可同时出现,get, set也可同时出现
    // 但当 value 和 writable 出现任意一个时,不可以配置 get, set,反之亦然
    get() {}
    set() {}
})

7. 判断对象是否有某个自有属性 [ [ HasProperty ] ]

// js实现: hasOwnProperty
var obj = { a: 1 }
Object.setPrototypeOf(obj, { b: 2 })
obj.hasOwnProperty(''a'') // true
obj.hasOwnProperty(''b'') // false

8. [ [ GET ] ]

// js实现: 关键字 in, obj.a 这种写法中的 . 等,都是GET的实现

9. [ [ SET ] ]

// js实现: obj.a = 1, obj[''a''] = 1 这种写法都是SET的实现

10. [ [ Delete ] ]

// js实现: 关键字 delete

11. [ [ Enumerate ] ]

// js实现: 关键字组合 for in

12. [ [ OwnPropertyKeys ] ]

// js实现: keys
var obj = { a: 1, b: 2 }
Object.setPrototypeOf(obj, { c: 3 })
Object.keys(obj) // [''a'', ''b'']

13. 函数调用

// js实现: 执行函数声明和执行函数表达式
function fn() {}    fn() // 执行函数声明
var obj = {
    fn = function() {}
}
obj.fn() // 执行函数表达式

14. 实例化对象

// js实现: new 关键字

JavaScript – 从Bootstrap的导航标签跳到另一个选项卡的特定选项卡

JavaScript – 从Bootstrap的导航标签跳到另一个选项卡的特定选项卡

我使用Bootstrap的导航标签与以下设置:
<ul>
  <li><a href="#home" data-toggle="tab">Home</a></li>
  <li><a href="#profile" data-toggle="tab">Profile</a></li>
</ul>
<div id="tabContent">
  <divid="home">
    <form id="tab">
        <label>Name:</label>
        <input type="text" value="fooBar">
    </form>
  </div>
  <divid="profile">
    <form id="tab2">
        <a href="#home">Home</a>
    </form>
  </div>
</div>

您可以看到,我的个人资料标签中有一个链接,链接到第一个标签.点击锚点会更改URL栏中的URL,但不跳转到特定选项卡.

然后我注意到通常不可能直接链接到选项卡,所以我从Twitter Bootstrap Tabs: Go to Specific Tab on Page Reload or Hyperlink添加了以下代码:

// Javascript to enable link to tab
var url = document.location.toString();
if (url.match('#')) {
    $('.nav-tabs a[href=#'+url.split('#')[1]+']').tab('show') ;
} 

// Change hash for page-reload
$('.nav-tabs a').on('shown',function (e) {
    window.location.hash = e.target.hash;
})

现在我可以从别的地方链接到一个特定的标签,但是如果我在导航栏的同一页面上,则不能.重新载入页面然后会跳转到所需的标签,所以我以为我可以强制重新加载页面,解决方案从Javascript: How to truly reload a site with an anchor tag?:

window.location.reload(true);

然而,每次点击一个标签时,它都会重新加载,此外,当点击锚点时,它仍然没有加载主页选项卡.

因此,我将如何从另一个标签跳转到给定的ID?

解决方法

你可能已经把错误的脚放在你提到的其他答案上了吗?从同一个页面中更改标签是不重要的(不管你是否在另一个选项卡中):你可以使用基本的引导 tab navigation Javascript为此.

首先改变你的html:添加一个id到你的< ul class =“nav nav-tabs”id =“myTab”> ..然后添加一个链接到你的第二个选项卡:

<divid="profile">
  <form id="tab2">
    <a href="#" id="gotohome">Jump to home tab (this works)</a>
...

并在您的文档中添加以下内容准备好:

$('#gotohome').click(function() {
  $('#myTab a[href="#home"]').tab('show');
});

jsFiddle工作示例.

javascript – 从iphone中的多选框中取消选择最后一个选项

javascript – 从iphone中的多选框中取消选择最后一个选项

我使用以下 jquery代码来限制多选框中的选择.它在任何 Android设备上工作正常但在iphone中出现问题.

在iphone中显示警报消息,但未按预期取消选择最后选择的元素.它应显示所选的3个项目,但它显示在iphone中发生警报消息后选择的4个项目.任何暗示赞赏.提前致谢.

<select id="mob-industry">
  <option value="1">Web Development</option>
  <option value="2">Architecture</option>
  <option value="3">Software Development</option>
  <option value="4">Hardware</option>
</select>


var last_valid_selection = null;
$('#mob-industry').change(function(event) {
    if ($(this).val().length > 3) {
        sweetAlert("","Only 3 Allowed","info");
          $(this).val(last_valid_selection);
    } else {
          last_valid_selection = $(this).val();
    }
 });

解决方法

我猜你的方法来自: How do you limit options selected in a html select box?

但我认为这是你真正想要/应该使用的:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

<select id="mob-industry" multiple>
    <option value="1">Web Development</option>
    <option value="2">Architecture</option>
    <option value="3">Software Development</option>
    <option value="4">Hardware</option>
</select>

$('#mob-industry').bind("vmousedown",function(event) {
    var num_selected = $(this).find(":selected").length;
    if (num_selected == 3) {
        var last_selected_value = $(event.target).val();
        var select_array = $(this).val();
        if ($.inArray(last_selected_value,select_array) == -1) {
            alert("YOU SHALL NOT PASS!!!");
            event.preventDefault();
            return false;
        }
    }
});

小提琴(使用mousedown()而不是.bind(“vmousedown”)与计算机兼容):https://jsfiddle.net/kpm0yLyt/1/

JsDoIt(移动模拟,虽然有一些奇怪的错误,当你选择它们时显示选项.忽略它.):http://jsdo.it/Macainian/GKxn

哦顺便说一句,替代方法(使用click()而不是.bind(“vclick”)与计算机兼容):https://jsfiddle.net/m49wr4t1/5/

关于Javascript最有效的方法来从ID链接的2个选项卡构建对象js获取id对应的组件的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于4种JavaScript实现简单tab选项卡切换的方法_javascript技巧、ECMAScript委员会:JavaScript最基础的14种操作对象的方法、JavaScript – 从Bootstrap的导航标签跳到另一个选项卡的特定选项卡、javascript – 从iphone中的多选框中取消选择最后一个选项的相关知识,请在本站寻找。

本文标签: