我需要具有story
属性的元素数组(在href
内部)以view.php
开头。
这是我的尝试,没有成功:
let arr = $('#story').find(".el[href.startsWith('view.php')]");
//let arr = $('#story').find(".el[attr('href').startsWith('view.php')]");
console.log(arr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id='story'>
<a class='el' href = 'view.php?id=323'>lorem</a>
<a class='el' href = 'about.php'>ipsum</a>
<a class='el' href = 'index.php'>lorem</a>
<a class='el' href = 'view.php?id=525'>ipsum</a>
</div>
答案 0 :(得分:6)
您可以使用a[href^="view.php"]
从属性选择器开始,然后通过href
和map
方法获取get
值的数组。
let arr = $('#story a[href^="view.php"]').map(function() {
return $(this).attr('href')
}).get()
console.log(arr)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id='story'>
<a class='el' href='view.php?id=323'>lorem</a>
<a class='el' href='about.php'>ipsum</a>
<a class='el' href='index.php'>lorem</a>
<a class='el' href='view.php?id=525'>ipsum</a>
</div>
答案 1 :(得分:3)
private Bitmap rescaleBitmap(Bitmap bitmap) {
float constant = 600.0f / bitmap.getWidth();
return Bitmap.createScaledBitmap(bitmap, (int) (bitmap.getWidth() * constant),
(int) (bitmap.getHeight() * constant), true);
}
答案 2 :(得分:1)
在这种情况下,此命令会打2遍问候。
let arr = [];
$('a[href^="view.php"]').each(function() {
arr.push(this.getAttribute("href"));
});
console.log(arr[0])
console.log(arr[1])
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id='story'>
<a class='el' href = 'view.php?id=323'>lorem</a>
<a class='el' href = 'about.php'>ipsum</a>
<a class='el' href = 'index.php'>lorem</a>
<a class='el' href = 'view.php?id=525'>ipsum</a>
</div>
答案 3 :(得分:0)
尝试一下:
const links = document.getElementsByClassName('el')
const views = Array.prototype.filter.call(
links,
el => el.getAttribute('href').startsWith('view')
);
console.log(views)
<div id='story'>
<a class='el' href = 'view.php?id=323'>First view link</a>
<a class='el' href = 'about.php'>ipsum</a>
<a class='el' href = 'index.php'>lorem</a>
<a class='el' href = 'view.php?id=525'>Second view link</a>
</div>