2016-06-16 4 views
-1

jqueryの内の文字列から特定の文字を取得するには、ので、私によると唯一のサブストリングは、次のHTMLから特定のデータを取得するためのオプションです:実ははどのように「のattr」を使用することはできません、私はhtmlの次き

<video poster="images/IMG_4979.jpg" controls=""><source type="video/mp4" src="video/IMG_4979.mp4"></source></video> 

、私はポスタータグのデータを取得したい。私は次の方法で試しましたが、常に間違ったデータを返します。

var pswp.currItem.html = '<video poster="images/IMG_4979.jpg" controls=""><source type="video/mp4" src="video/IMG_4979.mp4"></source></video>' 
var misc = pswp.currItem.html.substring(0, pswp.currItem.html.indexOf('controls')); 
alert(misc) 

理解しやすいかもしれないように、私はここにVARとして「pswp.currItem.html」を作成しましたが、それはattrがある上、この「pswp.currItem.html」オブジェクトから来ています働いていない。

私はどのようにポスタータグのデータを取得できますか教えてください。 (画像パス)

+0

@downvoters、それはように、あなたの貴重なコメントを残してください私は良質の次の時間に尋ねるのは簡単だろう –

答えて

3

文字列をJQueryオブジェクトにキャストできます。

私は理解しやすいように1つのステップを分けました。

// this is your HTML string 
 
var html_str = '<video poster="images/IMG_4979.jpg" controls=""><source type="video/mp4" src="video/IMG_4979.mp4"></source></video>'; 
 

 
// now we create a JQuery object from your string 
 
var $jq_obj = $(html_str); 
 

 
// now we can access the JQuery object as if it were part of the DOM 
 
var poster = $jq_obj.attr('poster'); 
 

 
// log the attribute 
 
console.log(poster);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

+0

おかげで、それは私に別の新しいことを教えて.. :) –

-1

attrを使用できないのはなぜですか?なぜオブジェクトに値を代入しようとしていますか?

var posterTag = $('video').attr('poster'); 
0

pswp.currItem.html文字列フォーマットは同じままなら、これは動作します。

var poster_image_path = pswp.currItem.html.split('"')[1] 
0

attr属性は、カスタムオブジェクトpswp.currItem.htmlに取り組むことができませんでしたが、あなたのラップはjQueryを使って、この文字列の場合、それは確かに動作します:

var poster = $(pswp.currItem.html).attr('poster'); 
関連する問題