خرید بک لینک

Vote count: 0

I am trying to capture just the opening tag of an HTML element. I am using element.outerHTML to get the string of text to work with. It works when there is a new line after the opening tag:

var div = document.querySelector('div');

console.log(
   div.outerHTML.match(/^<(.*)>/)[1]
);
<div id="awesomeID" class="one two three four">
</div>

However, when the element is a 1-liner, it breaks and captures all the way to the end of the closing tag:

var div = document.querySelector('div');

console.log(
  div.outerHTML.match(/^<(.*)>/)[1]
);
<div id="awesomeID" class="one two three four"></div>

How can I get this to only capture the opening tag?

asked 51 secs ago

- - , .
.

برچسب: نویسنده: استخدام کار تاريخ: يکشنبه 27 تير 1395 ساعت: 12:07

صفحه بندی