2016-07-20 1 views
0

저는 JQuery 1.10.2를 사용하고 있으며 다중 선택기로 시작하는 모든 링크를 찾으려고합니다. 최적화 할 수는 있지만 확신 할 수는 없습니다. 어떻게 알아. 선택기는 해당 URL로 시작하는 모든 링크를 반환해야합니다여러 셀렉터에서 대/소문자를 무시하고 찾기 시작 사용

:

valNow.find("a[href^='/GalleryDetail.aspx'], a[href^='/gallerydetail.aspx'], a[href^='GalleryDetail.aspx'], a[href^='gallerydetail.aspx'], 
a[href^='/sketchbook.aspx'], a[href^='/SketchBook.aspx'], 
a[href^='sketchbook.aspx'], a[href^='SketchBook.aspx']"); 

내가 검색, 난 내가 할 수있는 것을 읽을 : 지금이 순간에

/GalleryDetail.aspx 
/gallerydetail.aspx 
GalleryDetail.aspx 
gallerydetail.aspx 
/sketchbook.aspx 
/SketchBook.aspx 
SketchBook.aspx 
sketchbook.aspx 

을, 나는 이것을 사용하고 있습니다 필터를 사용하지만 다중 선택기이기 때문에 어디서부터 시작해야할지 모릅니다. 그리고 나는 이것을 사용하여 무시 상자를 수행 할 수 있다고 생각했습니다.

valNow.find("a[href^='/[gG]allery[dD]etail.aspx']") 

하지만 어떻게 든 링크를 반환하지 않습니다.

는 어떤 도움을 크게

을 감상 할 수있다

답변

1

, 그것은 또한없이 픽업 할 가능성 Array.prototype.filter (그리고 Array.prototype.some)와 가능한 해결

$("a").filter((_, el) => { 
 
     var startsWith = ["/gall", "sketch"], // values to look for 
 
      href = (el.getAttribute("href") || "").toLowerCase(); // the href value as defined in the markup 
 
    
 
     return startsWith.some((val) => href.indexOf(val) == 0); // returns true if the current elements (<el>) href attribute starts with (== 0) any of the values in <startsWith> 
 
//  return startsWith.some((val) => href.indexOf(val) == 0 || href.indexOf("/" + val) == 0) 
 
     }) 
 
     .addClass("foo");
.foo { background-color: yellow }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<a href="/GalleryDetail.aspx">/GalleryDetail.aspx</a> 
 
<a href="/gallerydetail.aspx">/gallerydetail.aspx</a> 
 
<a href="GalleryDetail.aspx">GalleryDetail.aspx</a> 
 
<a href="gallerydetail.aspx">gallerydetail.aspx</a> 
 
<a href="/SketchBook.aspx">/SketchBook.aspx</a> 
 
<a href="/sketchbook.aspx">/sketchbook.aspx</a> 
 
<a href="SketchBook.aspx">SketchBook.aspx</a> 
 
<a href="sketchbook.aspx">sketchbook.aspx</a>

+0

당신 안드레아스 감사입니다 // "startsWith = ["/ gall ","gall ","/ sketch ","sketch "]' – puntapret

+0

엄밀히 말하면 이것은"더 이상 "으로 시작하지 않고"아마도 " 스타트 s with "라고 표시하고 있지만 가능합니다 :) 나는 코드에서 ('과'/ '을 찾는 주석 처리 된 두 번째 리턴) 대안을 추가했습니다. 이 변경으로'startsWith' 배열을'[ "gall", "sketch"]'로 줄일 수 있습니다. [Here] (https://jsfiddle.net/h04cdwd7/)는 변경된 바이올린입니다. – Andreas