簡體   English   中英

使用 Javascript 從 json stingrigy 值中獲取 HTML 元素值

[英]Fetch HTML element value from json stingrigy value using Javascript

我有使用 JSON.stingrify 轉換的對象

{
  "_originalElementInner": "            <input type=\"file\" name=\"\" class=\"hidden\" id=\"file_input_two\" onchange=\"alertFilename(this.value,'cropped_two');\">                           <!-- <img src=\"https://dxyz.com/tmp/5bd432ed2d8dd_img3.jpg\" alt=\"\" width=\"100%\" height=\"100%\"> -->                                         "
}

從上面我想獲取“ cropped_two ”,這是在

onchange=\\"alertFilename(this.value,' cropped_two ');

這里this.value將保持不變,所以如果要拆分 string 。

我通過循環和其他方式嘗試了多種方式,但徒勞無功。

任何人都可以幫忙嗎? 提前致謝。

我想你只想要字符串? 我想這會奏效。

 let o = { "_originalElementInner": " <input type=\\"file\\" name=\\"\\" class=\\"hidden\\" id=\\"file_input_two\\" onchange=\\"alertFilename(this.value,'cropped_two');\\"> <!-- <img src=\\"https://dxyz.com/tmp/5bd432ed2d8dd_img3.jpg\\" alt=\\"\\" width=\\"100%\\" height=\\"100%\\"> --> " }; const regex = /,(.*)\\);/gm; const match = regex.exec(o._originalElementInner); console.log(match[1]);

您可以使用搜索函數找到“this.value”位置,然后使用從“this.value”位置+ 11到“)”位置的子字符串函數,似乎是字符串中唯一的“)”。

如果可以使用 jQuery,則可以遵循此方法。

 var obj = {"_originalElementInner": " <input type=\\"file\\" name=\\"\\" class=\\"hidden\\" id=\\"file_input_two\\" onchange=\\"alertFilename(this.value,'cropped_two');\\"> <!-- <img src=\\"https://dxyz.com/tmp/5bd432ed2d8dd_img3.jpg\\" alt=\\"\\" width=\\"100%\\" height=\\"100%\\"> --> "}, stringValue = '', // This function will be called when the event change is triggered. alertFilename = function(_, str) { stringValue = str;}; // Create the jQuery object and trigger the event change // Finally remove the function from window object (This is just to remove an unsed function after execute it). $(obj['_originalElementInner']).trigger('change'), (delete window['alertFilename']); console.log(stringValue);
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

如果您的代碼中可能有多個onchange回調,以下代碼將獲得引用alertFilename ,它也比 regexp 更快。

function extractSecondArg(text) {
  // Define the pattern to look for.
  const pattern = 'onchange=\"alertFilename(this.value,'

  // Find the index of the pattern.
  const patternIndex = text.indexOf(pattern);

  if (patternIndex !== -1) {
    // Find the closing parenthesis.
    const patternEnd = ');';
    const patternEndIndex = text.indexOf(patternEnd, patternIndex);

    // Extract the second argument of the function.
    return text.substring(patternIndex + pattern.length, patternEndIndex).replace(/["']+/g, '');
  }
}

這是一個如何使用它的示例。

const json = {
  "_originalElementInner": "            <input type=\"file\" name=\"\" class=\"hidden\" id=\"file_input_two\" onchange=\"alertFilename(this.value,'cropped_two');\">                           <!-- <img src=\"https://dxyz.com/tmp/5bd432ed2d8dd_img3.jpg\" alt=\"\" width=\"100%\" height=\"100%\"> -->                                         "
};

// Extract element to search in.
const secondArg = extractSecondArg(json['_originalElementInner']);
console.log(secondArg)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM