2013-11-04 53 views
0

要更改指定的标签任意打开文件,我试图再次保存该文件。html文件打开并保存jquery标签替换

对不起,我使用了翻译 jquery标签如何取代...?

的test.html↓

<banner_0101> 
     <div class="banner"><a href="#" rel="external"><img src="test.jpg" ></a></div> 
</banner_0101> 

<banner_0127> 
    <div class="banner"><img src="http://test.jpg" ></div> 
</banner_0127> 

test.php的↓

<div id="form"> 
    <form action="testok.php" method="POST"> 
     <div> 
      <label for="cate">CATE_NUMBER</label> 
      <input type="text" name="cate" id="cate"> 
     </div> 
     <div> 
      <label for="href">A href </label> 
      <input type="text" name="href" id="href"> 
     </div> 
     <div> 
      <label for="imgsrc">Img src</label> 
      <input type="text" name="imgsrc" id="imgsrc"> 
     </div> 
     <div> 
      <input type="submit" value="OK"> 
     </div> 
    </form> 
</div> 

ex) 
POST Value 
test.ok -> 
$_POST['cate'] -> banner_0101 
$_POST['href'] -> /test1.php 
$_POST['imgsrc'] -> testok.jpg 

test.html ↓ 

<banner_0101> 
     <div class="banner"><a href="/test1.php" rel="external"><img src="testok.jpg" ></a></div> 
</banner_0101> 

**How do I resolve this problem?** 

回答

0

之一,是许多解决方案(寻找特定的旗帜和更换使用.find().replaceWith())可以是这样的DOM而不去PHP和只是在jQuery中添加此脚本中:

$('#form form').on('submit', function(e){ 

    e.preventDefault(); //Stops your form default behavior 

    banner = $('#cate').val(); //banner value 
    link = $('#href').val(); //link 
    imgsrc = $('#imgsrc').val(); //img src 

    newDom = $('<'+ banner +'>').html($('<div>' , {'class' : 'banner'}).append($('<a>' ,{'href' : link }).append($('<img>',{'src': imgsrc })))); //your new HTML 

    $('body').find(banner).replaceWith(newDom); //find the specific banner and replace existing banner with new DOM 


});