使用 Javascript 解析 HTML 字符串以获取元素内容

发布于 2024-10-06 05:00:01 字数 2044 浏览 0 评论 0 原文

我有以下包含多个元素的字符串:

'<span class="fn" id="fn1">Matthew</span>
<span class="spouse" id="spouse1">Evelyn Ross</span>
<span class="bday" id="bday1">1456</span>
<span class="wday" id="wday1"></span>
<span class="dday" id="dday1">2000</span>'

将其解析为 5 个变量的最佳方法是什么?

编辑

为了清楚起见,这里是完整的代码,我正在为 jEditable 创建一个自定义输入类型,它允许我内联编辑 vcard。

   $.editable.addInputType('person', {
    element : function(settings, original) {     
   var fn  = $('<input id="fn_"/>');
  var bday  = $('<input id="bday_" />');
  var wday  = $('<input id="wday_" />');
  var dday  = $('<input id="dday_" />');
  var spouse  = $('<input id="spouse_" />');

  $(this).append(fn);
  $(this).append(bday);
  $(this).append(wday);
  $(this).append(dday);
  $(this).append(spouse);

        /* Hidden input to store value which is submitted to server. */
        var hidden = $('<input type="hidden">');
        $(this).append(hidden);
        return(hidden);
    },

 content : function(string, settings, original) {
   var $data = $(string);

   var data = {};
   $data.each(function () {
       var $t = $(this);
       data[$t.attr('id')] = {
                              class: $t.attr('class'),
                              value: $t.text()};
   });

   alert(data.length());

   $("#fn_", this).val('Name');
   $("#bday_", this).val('Born');
   $("#wday_", this).val('Married');
   $("#dday_", this).val('Died');
   $("#spouse_", this).val('Spouse');
     },

  submit: function (settings, original) {
  var value =  "<span class=fn>" + $("#fn_").val() + '</span>' + '<span class=spouse>' + $("#spouse_").val() + '</span>' + '<span class=bday>' + $("#bday_").val() + '</span>' + '<span class=wday>' + $("#wday_").val() + '</span>' +'<span class=dday>' + $("#dday_").val() + '</span>';
      $("input", this).val(value);
  }

});

I have the following string that contains several elements:

'<span class="fn" id="fn1">Matthew</span>
<span class="spouse" id="spouse1">Evelyn Ross</span>
<span class="bday" id="bday1">1456</span>
<span class="wday" id="wday1"></span>
<span class="dday" id="dday1">2000</span>'

What would be the best way to parse this out to 5 variables?

EDIT

For clarity, here's the full code, I'm creating a custom input type for jEditable that allows me to inline edit a vcard.

   $.editable.addInputType('person', {
    element : function(settings, original) {     
   var fn  = $('<input id="fn_"/>');
  var bday  = $('<input id="bday_" />');
  var wday  = $('<input id="wday_" />');
  var dday  = $('<input id="dday_" />');
  var spouse  = $('<input id="spouse_" />');

  $(this).append(fn);
  $(this).append(bday);
  $(this).append(wday);
  $(this).append(dday);
  $(this).append(spouse);

        /* Hidden input to store value which is submitted to server. */
        var hidden = $('<input type="hidden">');
        $(this).append(hidden);
        return(hidden);
    },

 content : function(string, settings, original) {
   var $data = $(string);

   var data = {};
   $data.each(function () {
       var $t = $(this);
       data[$t.attr('id')] = {
                              class: $t.attr('class'),
                              value: $t.text()};
   });

   alert(data.length());

   $("#fn_", this).val('Name');
   $("#bday_", this).val('Born');
   $("#wday_", this).val('Married');
   $("#dday_", this).val('Died');
   $("#spouse_", this).val('Spouse');
     },

  submit: function (settings, original) {
  var value =  "<span class=fn>" + $("#fn_").val() + '</span>' + '<span class=spouse>' + $("#spouse_").val() + '</span>' + '<span class=bday>' + $("#bday_").val() + '</span>' + '<span class=wday>' + $("#wday_").val() + '</span>' +'<span class=dday>' + $("#dday_").val() + '</span>';
      $("input", this).val(value);
  }

});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

在巴黎塔顶看东京樱花 2024-10-13 05:00:01

创建一个元素,使用 innerHTML 将其放入该元素,并使用 querySelector 选择它们。

因此,假设您只想使用普通的旧 js:

el = document.createElement('p');
el.innerHTML = '<span class="fn" id="fn1">Matthew</span> ... <span class="dday" id="dday1">2000</span>'
el.querySelector('.fn').innerText // = 'Matthew'
el.querySelector('#fn1').outerHTML // = "<span class="fn" id="fn1">Matthew</span>"

这几乎直接转换为 jQuery:

el = $('<p />').html('<span class="fn" id="fn1">Matthew</span> ... <span class="dday" id="dday1">2000</span>');
el.find('.fn').text() // = 'Mathew'

Create an element, put it into that element with innerHTML and select them out with querySelector.

So assuming you wanted to use just plain old js:

el = document.createElement('p');
el.innerHTML = '<span class="fn" id="fn1">Matthew</span> ... <span class="dday" id="dday1">2000</span>'
el.querySelector('.fn').innerText // = 'Matthew'
el.querySelector('#fn1').outerHTML // = "<span class="fn" id="fn1">Matthew</span>"

Which translates almost directly into jQuery:

el = $('<p />').html('<span class="fn" id="fn1">Matthew</span> ... <span class="dday" id="dday1">2000</span>');
el.find('.fn').text() // = 'Mathew'
仙女山的月亮 2024-10-13 05:00:01

首先找到元素,然后使用 attr()text()

您可以使用常用的 选择器 字符串或您在问题中提到的原始字符串来查找元素。

var target_string = # ... raw string or selector for '#interesting span'

var $data = $(target_string);

# adjust this to produce the format you want
var data = {};
$data.each(function () {
    var $t = $(this);
    data[$t.attr('id')] = {id: $t.attr('id'),
                           class: $t.attr('class'),
                           value: $t.text()};  # or .html()
});

# you can access this example data via the id values in the html
data['spouse'] # or
data.spouse

First find the elements then grab data from each element using a combination of attr() and text() or html().

You can find the elements by using the usual selector string or the raw string you mentioned in the question.

var target_string = # ... raw string or selector for '#interesting span'

var $data = $(target_string);

# adjust this to produce the format you want
var data = {};
$data.each(function () {
    var $t = $(this);
    data[$t.attr('id')] = {id: $t.attr('id'),
                           class: $t.attr('class'),
                           value: $t.text()};  # or .html()
});

# you can access this example data via the id values in the html
data['spouse'] # or
data.spouse
高冷爸爸 2024-10-13 05:00:01

使用 jQuery,查看 text()

// get text from all spans
var all_text = $('span').text();
// break out into an array of values
var values = all_text.split(' ');

注意 这将得到 span 标记的 >text。因此,您将想要制作一个更好的选择器,并且可能以比 split() 更好的方式将其分配给变量。

Using jQuery, check out text()

// get text from all spans
var all_text = $('span').text();
// break out into an array of values
var values = all_text.split(' ');

NOTE this will get the text for all span tags on the page. So you will want to make a better selector and probably assign this to variables in a better way than split().

嘿看小鸭子会跑 2024-10-13 05:00:01

使用 jQuery 的 .html
        

Use jQuery's .html.
          

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文