获取元素的价值-如果失败则获取InnerHTML

时间:2019-03-02 04:43:45

标签: javascript

基本上,我正在创建一个chrome扩展程序,该扩展程序可以从网页中提取数据以加快数据输入速度。我正在尝试让用户尽可能容易地进行操作,这让我自己有些头痛。我允许他们为要查找的字段添加一个ID或名称,最后,我必须确定这是输入还是仅仅是他们想提取的文本,并查看该元素是否存在ID,如果没有,请检查以其提供的值作为名称的元素。必须使用它们提供的多个字段来完成。

我已经尝试了几次代码迭代来做到这一点。目前,我一直试图在下面获取此代码来实现此目的,但显然无法正常工作。是否有更好的方法仅通过javascript完成此操作?

var msg = {
  "first": "first_name_id_name",
  "last": "last_name_id_name",
  "email": "email_id_name",
  "phone": "phone_id_name"
}
var fields_to_find = JSON.parse(msg);
var data_to_push = {};
var current;
var ele;
for (var key in fields_to_find) {
  if (fields_to_find.hasOwnProperty(key)) {
    console.log(fields_to_find[key]);
    ele = document.getElementById(fields_to_find[key]);
    if (!ele.value) {
      ele = document.getElementById(fields_to_find[key]);
      if (ele.innerHTML.length == 0) {
        ele = document.getElementsByName(fields_to_find[key]);
        if (!ele.value) {
          ele = document.getElementsByName(fields_to_find[key]);
          if (ele.innerHTML.length == 0) {
            current = "";
          } else {
            current = ele.innerHTML;
          }
        } else {
          current = ele.value;
        }
      } else {
        current = ele.innerHTML;
      }
    } else {
      current = ele.value;
    }
    data_to_push[key] = current;
  }
}
console.log(data_to_push);
<input type="text" id="first_name_id_name" value="John">
<p name="last_name_id_name">Doe</p>
<input type="text" name="email_id_name" value="johndoe@example.com">
<p id="phone_id_name">123-555-1234</p>

您所能提供的任何帮助都将是非常棒的,并且会受到赞赏。

感谢您的时间!

2 个答案:

答案 0 :(得分:1)

您可以剥离许多多余的嵌套if语句,并使用如下所示的几个备份值。另外,由于JSON是JS中的本机对象,因此您不必在JSON.parse对象上进行msg

var msg = {
  first: "first_name_id_name",
  last: "last_name_id_name",
  email: "email_id_name",
  phone: "phone_id_name"
};
var data_to_push = {};
var current;
var ele;
for (var key in msg) {
  if (msg.hasOwnProperty(key)) {
    console.log(msg[key]);
    ele = document.getElementById(msg[key]) || document.getElementsByName(msg[key])[0];
    if (ele) {
      current = ele.value || ele.innerHTML || '';
    } else {
      current = '';
    }
    data_to_push[key] = current;
  }
}
console.log(data_to_push);
<input type="text" id="first_name_id_name" value="John">
<p name="last_name_id_name">Doe</p>
<input type="text" name="email_id_name" value="johndoe@example.com">
<p id="phone_id_name">123-555-1234</p>

答案 1 :(得分:0)

1:对library(httr) library(XML) library(data.table) library(qdap) library(Hmisc) getwsj.quotes <- function(Symbol) { MyUrl <- sprintf("https://quotes.wsj.com/AU/XASX/%s/financials/annual/income-statement", Symbol) Symbol.Data <- GET(MyUrl) x <- content(Symbol.Data, as = 'text') wsj.tables <- sub('cr_dataTable cr_sub_capital', '\\1', x) SymData <- readHTMLTable(wsj.tables) return(SymData) } TickerList <- c("AMC") SymbolDataList <- lapply(TickerList, FUN = getwsj.quotes) MySymList <- data.frame() MySymList <- SymbolDataList[[1]][[2]] Search(MySymList, " Net Income") 变量使用'

2:您不需要很多条件。

有很多不正确的代码,要查看所有更改,请检查完整代码:

msg
var msg = '{ "first": "first_name_id_name","last": "last_name_id_name","email": "email_id_name",  "phone": "phone_id_name"}' ;

var fields_to_find = JSON.parse( msg ), 
    data_to_push = {}, 
    current , 
    ele ;

for ( var key in fields_to_find ) {

  if ( fields_to_find.hasOwnProperty ( key ) ) {

    console.log( fields_to_find[key] );

    ele = document.getElementById( fields_to_find[key] ) ? document.getElementById( fields_to_find[key] ) : document.getElementsByName( fields_to_find[key] )[0];
    
    if ( ele ) { data_to_push[key] = ( ele.innerHTML ) ? ele.innerHTML : ( ele.value ? ele.value : '' ) ; }

  }

}

console.log(data_to_push);