如何使用js选择不在类内的数据?

时间:2016-03-04 04:00:04

标签: javascript jquery html css

我想从html表单中选择数据,如下所示:

<p class="company-detail">                      
  <span class="sb-span-left">Industry</span> : Recruiting Services<br>
  <span  class="sb-span-left">Telephone</span> : 023 993 009<br>
  <span class="sb-span-left">Email</span> : cv@camupjob.com<br>
  <span class="sb-span-left">Website</span>: www.camupjob.com<br>
</p>

如何选择获取**

行业:招聘服务

电话:023 993 009

电子邮件:cv@camupjob.com

网站:www.camupjob.com **

这是我的代码,但无法获得我想要的结果

var data1 = :$('.company-detail').find('.sb-span-left:contains("Industry")').next().text();

的console.log(DATA1);

还有一个

var data1 = :$('.company-detail').find('.sb-span-left:contains("Industry")').next().children().remove().end().text();
console.log(data1);

但两者都不起作用。请帮帮我&gt;提前谢谢

4 个答案:

答案 0 :(得分:3)

您可以使用全字regex,然后用空格替换单词:

&#13;
&#13;
var neededText = $('.company-detail').text().replace(/(Industry :| Telephone :|Email :|Website:)/g, '');
$("button").click(function() {
  $("d").html(neededText)
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<p class="company-detail">
  <span class="sb-span-left">Industry</span> : Recruiting Services
  <br>
  <span class="sb-span-left">Telephone</span> : 023 993 009
  <br>
  <span class="sb-span-left">Email</span> : cv@camupjob.com
  <br>
  <span class="sb-span-left">Website</span>: www.camupjob.com
  <br>
</p>
<button>Get data</button>

<d></d>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

另一种方法是通过正则表达式进行。

修改

var data = $('.company-detail').html();
var arr = ['Industry', 'Telephone', 'Email', 'Website'];
for(var i=0; i<arr.length; i++){
    data = data.replace(/<span class="sb-span-left">[a-zA-Z]*<\/span>/, arr[i]);
}
data = data.replace(/<br>/g, '');
console.log(data);

答案 2 :(得分:1)

尝试使用$(".company-detail br")选择器,$.map()Node.previousSiblingString.prototype.replace()RegExp /^(\s+|:)+\s/一起删除" : "或{{ 1}}在所选文本节点的开头,": "

&#13;
&#13;
.nodeValue
&#13;
var data = $.map($(".company-detail br"), function(el, i) {
  return el.previousSibling.nodeValue.replace(/^(\s+|:)+\s/, "")
});

console.log(data)
&#13;
&#13;
&#13;

或者使用<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <p class="company-detail"> <span class="sb-span-left">Industry</span> : Recruiting Services<br> <span class="sb-span-left">Telephone</span> : 023 993 009<br> <span class="sb-span-left">Email</span> : cv@camupjob.com<br> <span class="sb-span-left">Website</span>: www.camupjob.com<br> </p>Element.innerHTML.match() RegExp匹配/[0-9a-z\s.@]+(?=<br>)/ig元素之前的html中的字或数字字符,{{1 },<br>

&#13;
&#13;
.toString()
&#13;
.trim()
&#13;
&#13;
&#13;

答案 3 :(得分:1)

如果你只想要一个长字符串,就像这样简单:

console.log($.trim($(".company-detail").text()));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="company-detail">                      
  <span class="sb-span-left">Industry</span> : Recruiting Services<br>
  <span  class="sb-span-left">Telephone</span> : 023 993 009<br>
  <span class="sb-span-left">Email</span> : cv@camupjob.com<br>
  <span class="sb-span-left">Website</span>: www.camupjob.com<br>
</p>

这是一个更灵活的(和纯JS,没有jQuery),将span中的任何内容作为对象中的键,并使用以下文本节点作为值。

(function() {
  var parent = document.getElementsByClassName('company-detail')[0];
  var childNodes = parent.childNodes;
  var obj = {};
  var lastKey = null;

  // Iterate through each of the child nodes within the company detail element
  for (var i = 0; i < childNodes.length; i++) {
    if (childNodes[i].nodeType == 1) { // It's a tag
      if (childNodes[i].tagName.toUpperCase() == "SPAN") { // It's a span
        lastKey = childNodes[i].innerHTML.trim(); // Save the text inside of the span to be used as a key
        if (lastKey.length === 0) {
          lastKey = null;
        }
      }
    } else if (childNodes[i].nodeType == 3) { // It's a text node
      var val = childNodes[i].nodeValue.trim();
      val = val.split(":"); // Split up the string based on the ":" character

      // If there was a ":" in the string, take the text after it as the value, otherwise just take it as it is
      if (val.length > 1) {
        val = val[1].trim();
      } else {
        val = val[0];
      }

      // Save the value with the last key that was extracted
      if (lastKey && val.length > 0) {
        obj[lastKey] = val;
      }
    }
  }


  console.log(obj);
})();
<p class="company-detail">
  <span class="sb-span-left">Industry</span> : Recruiting Services
  <br>
  <span class="sb-span-left">Telephone</span> : 023 993 009
  <br>
  <span class="sb-span-left">Email</span> : cv@camupjob.com
  <br>
  <span class="sb-span-left">Website</span>: www.camupjob.com
  <br>
</p>