查找我点击了哪个 Class[i] 队列
Finding Which Class[i] queue I Click
我想知道我点击的是哪个类名[i] 数字。我想知道我点击时“i”是什么。我怎样才能得到这些数据?
function findclassquee(id) {
alert ("I am confused please help me about finding which one I am?; *<<<document.getElementsByClassName('trying')[1] or *document.getElementsByClassName('trying')[5]>>> ");
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button onclick="findclassquee(this.id)" class="trying">Which Class </button>
<button onclick="findclassquee(this.id)" class="trying">Which Class </button>
<button onclick="findclassquee(this.id)" class="trying">Which Class </button>
<button onclick="findclassquee(this.id)" class="trying">Which Class </button>
<button onclick="findclassquee(this.id)" class="trying">Which Class </button>
<button onclick="findclassquee(this.id)" class="trying">Which Class </button>
你可以这样做,indexOf
:
var elems = Array.from(document.querySelectorAll('.trying'));
// Listen for clicks on each element
elems.forEach(el => el.addEventListener('click', findclassquee));
function findclassquee() {
// Find the index of the clicked one
var index = elems.indexOf(this);
alert(`You clicked on the element at index ${index}`);
}
<button class="trying">Which Class</button>
<button class="trying">Which Class</button>
<button class="trying">Which Class</button>
<button class="trying">Which Class</button>
<button class="trying">Which Class</button>
<button class="trying">Which Class</button>
注意:我正在使用 Array.from()
将 NodeList 转换为数组,并且能够使用数组上面的方法,比如 indexOf
.
这是我想要的最好的,最后我发现:
$(".trying").click(function(){alert($(".trying").index(this));});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="trying">Which Class</button>
<button class="trying">Which Class</button>
<button class="trying">Which Class</button>
<button class="trying">Which Class</button>
<button class="trying">Which Class</button>
<button class="trying">Which Class</button>
我想知道我点击的是哪个类名[i] 数字。我想知道我点击时“i”是什么。我怎样才能得到这些数据?
function findclassquee(id) {
alert ("I am confused please help me about finding which one I am?; *<<<document.getElementsByClassName('trying')[1] or *document.getElementsByClassName('trying')[5]>>> ");
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button onclick="findclassquee(this.id)" class="trying">Which Class </button>
<button onclick="findclassquee(this.id)" class="trying">Which Class </button>
<button onclick="findclassquee(this.id)" class="trying">Which Class </button>
<button onclick="findclassquee(this.id)" class="trying">Which Class </button>
<button onclick="findclassquee(this.id)" class="trying">Which Class </button>
<button onclick="findclassquee(this.id)" class="trying">Which Class </button>
你可以这样做,indexOf
:
var elems = Array.from(document.querySelectorAll('.trying'));
// Listen for clicks on each element
elems.forEach(el => el.addEventListener('click', findclassquee));
function findclassquee() {
// Find the index of the clicked one
var index = elems.indexOf(this);
alert(`You clicked on the element at index ${index}`);
}
<button class="trying">Which Class</button>
<button class="trying">Which Class</button>
<button class="trying">Which Class</button>
<button class="trying">Which Class</button>
<button class="trying">Which Class</button>
<button class="trying">Which Class</button>
注意:我正在使用 Array.from()
将 NodeList 转换为数组,并且能够使用数组上面的方法,比如 indexOf
.
这是我想要的最好的,最后我发现:
$(".trying").click(function(){alert($(".trying").index(this));});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="trying">Which Class</button>
<button class="trying">Which Class</button>
<button class="trying">Which Class</button>
<button class="trying">Which Class</button>
<button class="trying">Which Class</button>
<button class="trying">Which Class</button>