从javascript中的rss描述中提取src

时间:2017-04-29 10:54:09

标签: javascript jquery rss

我的RSS说明如下:

  

'描述:< a href =" http://www.afaqs.com/news/story/50219_Management-changes-at-Disney-Vijay-Subramaniam-quits-Abhishek-Maheshwari- to-head-Disney-Media">< img src =" http://www.afaqs.com/all/news/images/news_story_grfx/2017/50219_1_home_small.jpg" ALT =" " HSPACE =" 5" ALIGN ="左"宽度=" 59"高度=" 59"边界=" 0" />< / A>该网络还将Sanjeet Mehta提升为迪士尼消费品行业的负责人。'

我必须在jquery中从中提取img src。

2 个答案:

答案 0 :(得分:0)

考虑这种方法:

  1. 使用ID创建div元素。
  2. 在您的javascript代码中,将InnerHTML值设置为div,其值为description。
  3. 使用querySelectorAll并获取图像的来源。
  4. 示例:

    
    
    id
    
    var description = 'description: <a href="http: //www.afaqs.com/news/story/50219_Management-changes-at-Disney-Vijay-Subramaniam-quits-Abhishek-Maheshwari-to-head-Disney-Media"><img src="http://www.afaqs.com/all/news/images/news_story_grfx/2017/50219_1_home_small.jpg" alt=" " hspace="5" align="left" width="59" height="59" border="0"/></a> The network has also elevated Sanjeet Mehta as head of Disney Consumer Products.';
    
    // Set InnerHTML values:
    document.getElementById('myDiv').innerHTML = description;
    
    // Get src:
    var mySrcValue = document.querySelectorAll('#myDiv > a > img')[0].src;
    
    // Show src value:
    document.getElementById('myResult').innerHTML = "<b>" + mySrcValue + "</b>";
    &#13;
    &#13;
    &#13;

答案 1 :(得分:0)

你不需要jquery这样做,javascript就足够了。 您可以使用一行代码(split和regex)提取src属性:

var result = description.split('img')[1].split('alt')[0].match( /"(.*?)"/ )[1];

var description = 'description: <a href="http: //www.afaqs.com/news/story/50219_Management-changes-at-Disney-Vijay-Subramaniam-quits-Abhishek-Maheshwari-to-head-Disney-Media"><img src="http://www.afaqs.com/all/news/images/news_story_grfx/2017/50219_1_home_small.jpg" alt=" " hspace="5" align="left" width="59" height="59" border="0"/></a> The network has also elevated Sanjeet Mehta as head of Disney Consumer Products.';

result.innerHTML = description.split('img')[1].split('alt')[0].match( /"(.*?)"/ )[1];
<div id="result"></div>